簡體   English   中英

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

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

當計算機的選擇與玩家的選擇相同時,以下程序不會打印“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!")

我究竟做錯了什么?

計算機的選擇存儲在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:

如果您還有任何疑問,請在評論中提問,我會改進我的答案。 謝謝:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM