简体   繁体   English

该程序不打印它是平局。 我究竟做错了什么?

[英]The program is not printing it's a tie. What am I doing wrong?

The following program is not printing "It's a tie" when the computer's choice is the same as the player's choice.当计算机的选择与玩家的选择相同时,以下程序不会打印“It's a tie”。

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!")

What am I doing wrong?我究竟做错了什么?

The computer's choice is stored in the play_moves variable so you should compare user_play with play_moves to check if it's a tie:计算机的选择存储在play_moves变量中,因此您应该比较user_playplay_moves以检查它是否平局:

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

The program that you just wrote doesn't actually print the computer's random choice but instead it again prompts for user input one more time.您刚刚编写的程序实际上并没有打印计算机的随机选择,而是再次提示用户输入一次。

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

Instead you might wanted to write it like this -相反,你可能想这样写 -

print("Computer Choices : " + play_moves)

And moreover, at the end of the program, it doesn't check equality of the random computer guess ( play_moves ) and the user input ( user_play ) but instead it checks the equality of the second user input ( computer_play ) with the first input.此外,在程序结束时,它不会检查随机计算机猜测 ( play_moves ) 和用户输入 ( user_play ) 是否相等,而是检查第二个用户输入 ( computer_play ) 与第一个输入是否相等。

if user_play== computer_play:

So, to compare the user input and the computer's random guess you would do this -因此,要比较用户输入和计算机的随机猜测,您可以这样做 -

if user_play == play_moves:

Ask in the comments if you still have any doubts and I will improve my answer.如果您还有任何疑问,请在评论中提问,我会改进我的答案。 Thanks:)谢谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM