user_input_num = int(input("please select a number between 0 - 100 !"))
def jokes(moon,ocean,construction,tooth,circus):
moon = print(("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."))
ocean = print(("What did one ocean say to the other ocean? \n Nothing, it just waved."))
construction = print(("Do you want to hear a construction joke? \n Sorry, I’m still working on it."))
tooth = print(("What do dentists call their x-rays? \n Tooth pics!"))
circus = print(("Did you hear about the fire at the circus? \n It was in tents!"))
if (user_input_num > 0 and user_input_num < 21):
jokes(moon)
elif (user_input_num > 21 and user_input_num < 41):
jokes(ocean)
elif (user_input_num > 41 and user_input_num < 61):
jokes(construction)
elif (user_input_num > 61 and user_input_num < 81):
jokes(tooth)
elif (user_input_num > 81 and user_input_num < 101):
jokes(circus)
else: print("Error number not within specified range")
用戶應該可以輸入 1-100 之間的數字,然后會回傳一個笑話,笑話不列印幫助
uj5u.com熱心網友回復:
需要這樣
def jokes(user_input_num):
moon = "Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."
ocean = "What did one ocean say to the other ocean? \n Nothing, it just waved.")
construction = "Do you want to hear a construction joke? \n Sorry, I’m still working on it."
if (user_input_num > 0 and user_input_num < 21):
print(moon)
....
然后呼叫你的jokes方法
user_input_num = int(input("please select a number between 0 - 100 !"))
jokes(user_input_num)
邊注:
- 您也可以
if 0 < user_input_num < 21在您的條件下使用以獲得更好的可讀性 - 我試圖通過使用想出一種更優雅的方法,
dict但看起來if-else是處理范圍的最佳方法,在睡覺前要閱讀的東西https://stackoverflow.com/a/45075450/342553
uj5u.com熱心網友回復:
您必須呼叫該函式以使其執行其中的代碼。我建議修改引數以user_input_num使其更有意義和可運行。
user_input_num = int(input("please select a number between 0 - 100 !"))
def jokes(user_input_num):
moon = print(("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere."))
ocean = print(("What did one ocean say to the other ocean? \n Nothing, it just waved."))
construction = print(("Do you want to hear a construction joke? \n Sorry, I’m still working on it."))
tooth = print(("What do dentists call their x-rays? \n Tooth pics!"))
circus = print(("Did you hear about the fire at the circus? \n It was in tents!"))
if (user_input_num > 0 and user_input_num < 21):
jokes(moon)
elif (user_input_num > 21 and user_input_num < 41):
jokes(ocean)
elif (user_input_num > 41 and user_input_num < 61):
jokes(construction)
elif (user_input_num > 61 and user_input_num < 81):
jokes(tooth)
elif (user_input_num > 81 and user_input_num < 101):
jokes(circus)
else: print("Error number not within specified range")
jokes(user_input_num)
uj5u.com熱心網友回復:
您的縮進放錯了位置...只需將其洗掉,然后直接使用 print
user_input_num = int(input("please select a number between 0 - 100 !"))
if (user_input_num > 0 and user_input_num < 21):
print("Did you hear about the first restaurant to open on the moon? \n It had great food, but no atmosphere.")
elif (user_input_num > 21 and user_input_num < 41):
print("What did one ocean say to the other ocean? \n Nothing, it just waved.")
elif (user_input_num > 41 and user_input_num < 61):
print("Do you want to hear a construction joke? \n Sorry, I’m still working on it.")
elif (user_input_num > 61 and user_input_num < 81):
print("What do dentists call their x-rays? \n Tooth pics!")
elif (user_input_num > 81 and user_input_num < 101):
print("Did you hear about the fire at the circus? \n It was in tents!")
else: print("Error number not within specified range")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466202.html
標籤:Python
