import random
random_Number = random.randrange(1, 4)
support = input('Write help to support ')
if support == "help":
print("this is Rock, Paper, Scissors game ")
play_Game = input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n')
if random_Number == 1 and play_Game == 1 :
print('The game is draw\n Try Again!')
elif random_Number == 2 and play_Game == 1:
print('You lose!\n Hard luck')
elif random_Number == 3 and play_Game == 1:
print('You won!\n Good luck')
elif random_Number == 1 and play_Game == 2:
print('You won!\n Good luck')
elif random_Number == 2 and play_Game == 2:
print('The game is draw\n Try Again!')
elif random_Number == 2 and play_Game == 2:
print('The game is draw\n Try Again!')
elif random_Number == 3 and play_Game == 2:
print('You lose!\n Hard luck')
elif random_Number == 1 and play_Game == 3:
print('You lose!\n Hard luck')
elif random_Number == 2 and play_Game == 3:
print('You won!\n Good luck')
elif random_Number == 3 and play_Game == 3:
print('The game is draw\n Try Again!')
else:
print('please, enter a correct number ')
else:
print("write the true keyword")
评论:当我输入 1、2 或 3 时,它告诉我输入正确的数字,我认为错误在 if 语句中。我学过python,这是我的第一个项目。请告诉我这是一个干净的代码吗?
答案 0 :(得分:1)
这是因为您将字符串与整数进行比较。在 python input()
中返回一个字符串。当然,字符串永远不等于整数。
因此。使用 int()
play_Game=int(input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n'))
答案 1 :(得分:0)
在测试中保留字符串类型并用字符串替换数字可能更安全。这样,如果在提示中输入除数字以外的其他内容,程序就不会崩溃。
play_Game = input('-enter 1 to Rock\n-enter 2 Paper\n-enter 3 to Scissors \n')
if random_Number == "1" and play_Game == "1" :
print('The game is draw\n Try Again!')
否则,任何非数字输入都会在转换为 int 期间触发错误。