程序没有打印,而是领带。我做错了什么?

  • 本文关键字:错了 打印 程序 python
  • 更新时间 :
  • 英文 :


下面的程序没有打印"当计算机的选择与玩家的选择相同时。

import random
print("welcome to rock,paper, scissors and dynamite game!")
moves=["rock","paper","scissors","dynamite"]
for i in range(1,6):
print("Round",i)
user_play=input("player "+str(i)+" choice?").lower().strip(" ")
play_moves=random.choice(moves)
computer_play=input("computer choices:"+play_moves).lower().strip(" ")

if user_play== computer_play:
print("It's a tie!")

我做错了什么?

计算机的选择存储在play_moves变量中,因此您应该将user_playplay_moves进行比较,以检查是否平局:

if user_play == play_moves:
print("It's a tie!")

您刚刚编写的程序实际上并没有打印计算机的随机选择,而是再次提示用户输入。

computer_play=input("computer choices:"+play_moves).lower().strip(" ")

你可以这样写——

print("Computer Choices : " + play_moves)

此外,在程序结束时,它不检查随机计算机猜测(play_moves)与用户输入(user_play)是否相等,而是检查第二个用户输入(computer_play)与第一个输入是否相等。

if user_play== computer_play:

所以,为了比较用户输入和计算机的随机猜测,你会这样做-

if user_play == play_moves:

如果你还有任何疑问,请在评论中提问,我会改进我的答案。谢谢:)

相关内容

最新更新