我在從本教程中學習 python 時得到了一個練習,我得到了一個關于 Snake、Water 和 Gun 的練習并確實解決了它,但是程式只需要一個輸入然后停止。這是我正在使用的代碼:
import random
print("Welcome to Snake, Water or Gun!")
opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
c_ch = random.choice(opts)
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances < 10:
if u_ch == "Snake" and c_ch == "Snake":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
elif u_ch == "Snake" and c_ch == "Water":
chances -= 1
u_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif u_ch == 'Snake'and c_ch == "Gun":
chances -= 1
u_score = 1
print("You win 1 point.")
print(f"{chances} chances left.")
if u_ch == "Water" and c_ch == "Snake":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_ch == "Gun":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
if u_ch == "Gun":
if c_ch == "Snake":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Gun":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_score > u_score:
print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
我根本無法理解這個問題。
我想讓電腦玩一個完整的游戲,我也使用了隨機模塊,但它只在輸入后就停止了。這是代碼:
import random
print("Welcome to Snake, Water or Gun!")
opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
c_ch = random.choice(opts)
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances < 10:
if u_ch == "Snake" and c_ch == "Snake":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
elif u_ch == "Snake" and c_ch == "Water":
chances -= 1
u_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif u_ch == 'Snake'and c_ch == "Gun":
chances -= 1
u_score = 1
print("You win 1 point.")
print(f"{chances} chances left.")
if u_ch == "Water" and c_ch == "Snake":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_ch == "Gun":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
if u_ch == "Gun":
if c_ch == "Snake":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Gun":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_score > u_score:
print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
uj5u.com熱心網友回復:
看看你的代碼中的這兩行,他們會告訴你哪里出了問題!
...
chances = 10
while chances < 10:
...
uj5u.com熱心網友回復:
while chances < 10:我猜你想隨機做十次并根據
我修復你的代碼的結果來判斷分數,再試一次
import random
print("Welcome to Snake, Water or Gun!")
opts = ["Snake", "Water", "Gun"]
u_ch = input("Enter Snake, Water or Gun:")
chances = 10
ties = 0
u_score = 0
c_score = 0
while chances > 0:
c_ch = random.choice(opts)
if u_ch == "Snake" and c_ch == "Snake":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
elif u_ch == "Snake" and c_ch == "Water":
chances -= 1
u_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif u_ch == 'Snake'and c_ch == "Gun":
chances -= 1
u_score = 1
print("You win 1 point.")
print(f"{chances} chances left.")
if u_ch == "Water" and c_ch == "Snake":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_ch == "Gun":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
if u_ch == "Gun":
if c_ch == "Snake":
chances -= 1
u_score = 1
print("Player wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Water":
chances -= 1
c_score = 1
print("Computer wins 1 point.")
print(f"{chances} chances left.")
elif c_ch == "Gun":
chances -= 1
ties = 1
print("It is a tie.")
print(f"{chances} chances left.")
if c_score > u_score:
print(f"You lose!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if u_score > c_score:
print(f"You win!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
if c_score == u_score:
print(f"Tie!\nComputer score:{c_score}\nYour score:{u_score}\nTies:{ties}")
uj5u.com熱心網友回復:
該chances變數設定為 10,但只有小于 10 時才運行 while 回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451699.html
