所以我有一個猜數字游戲,我以前的所有代碼都可以正常作業,你可能會看到我在螢屏上列印了隨機生成的數字。但是當我猜對了數字時,我仍然會被告知我錯了?
num = randrange(0, 100)
if num > 0 and num < 50:
print("The number is greater than 0 but smaller than 50")
elif num > 50 and num < 100:
print("The number is smaller than 100 but greater than 50")
print(num)
print('Start Guessing')
a = input()
a = input()
if a == num:
print ("You got the correct number, woohoo")
elif num > 0 and num < 20:
print ("Hint: The number is bigger than 0 but smaller than 20")
elif num > 20 and num < 40:
print ('Hint: The number is bigger than 20 but smaller than 40')
elif num > 40 and num < 60:
print ('Hint: The number is bigger than 40 but smaller than 60')
elif num > 60 and num < 80:
print ('Hint: The number is bigger than 60 but smaller than 80')
elif num > 80 and num < 100:
print ('Hint: The number is bigger than 80 but smaller than 100')
對不起,看起來很奇怪的代碼,我以前從未使用過這個網站。
uj5u.com熱心網友回復:
您的代碼似乎是 Python。您應該在標簽中指出這一點。此外,由于 Python if 陳述句依賴于縮進,因此您應該使用適當的縮進發布代碼。
在代碼中:
a = input()
if a == num:
您正在將字串與數字進行比較。您應該a轉換為int.
if int(a) == num:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457682.html
