使用引數 num_feet 和 num_inches 定義一個函式 print_feet_inch_short(),使用 ' 和 " 簡寫形式列印。以換行符結尾。記住 print() 默認輸出換行符。例如:print_feet_inch_short(5, 8) 列印結果:5' 8 "
我的代碼:
def print_feet_inch_short(num_feet, num_inches):
return print(num_feet , num_inches )
''' Your solution goes here '''
user_feet = int(input())
user_inches = int(input())
print_feet_inch_short(user_feet, user_inches) # Will be run with (5, 8), then (4, 11)
當我編譯我的代碼時,我得到5 8的不是5' 8"
Please help me to get the inch and feet symbols in the function
提前致謝
uj5u.com熱心網友回復:
嘗試:
def print_feet_inch_short(num_feet, num_inches):
print(f"{num_feet}' {num_inches}\"")
用法:
>>> print_feet_inch_short(5, 8)
5' 8"
uj5u.com熱心網友回復:
def print_feet_inch_short(num_feet, num_inches):
#first, you cannot use both print and return in the same line
#here you have both examples using first the print statement
#Also, to obtain the result you need, you should use the f" string, which formats the string
print(f"{num_feet}' {num_inches}\"")
#Or you can use the .format() fucntion, and you get the same result
print("{}' {}\"".format(num_feet,num_inches))
#and the usung the return statement
return f"{num_feet}' {num_inches}\""
user_feet = int(input())
user_inches = int(input())
#When you call the function, only the print statement gets to run
print_feet_inch_short(user_feet, user_inches)
#that's because the return statement returns the value into the function itself
#To get the return statement to appear, you use the print statement
print(print_feet_inch_short(user_feet, user_inches))
uj5u.com熱心網友回復:
嘗試逃避它:
print("5' 8\"")
或使用您的變數:
print(f"{num_feet}' {num_inches}\"")
干杯
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428061.html
