我正在使用Python。目前是新的。我正在用win counter创建一个石头剪刀游戏。我在线查找一些解决方案,但它对我不起作用。我接近完成它。但是它只是一个错误。我无法让计数器工作。它没有显示任何胜利在计划结束时计算
from random import randint
print ("Rock ,Paper,Scissors game.")
#Function to get computer input
def generate():
comlist = ["rock","paper","scissors"]
comans = comlist[randint(-1,2)]
if comans == "rock":
print ("Computer choose rock.")
elif comans == "paper" :
print ("Computer choose paper.")
elif comans == "scissors":
print ("Computer choose scissors.")
return comans
#Function to get user input
def user():
userchoice = input ("Choose rock, paper , or scissors.")
while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors':
print ("Invalid input. Please enter again")
userchoice = input ("Choose rock, paper , or scissors.")
if userchoice == "rock":
print ("You choose rock.")
choice = userchoice
elif userchoice == "paper" :
print ("You choose paper.")
choice = userchoice
else:
userchoice == "scissors"
print ("You choose scissors.")
choice = userchoice
return choice
#Function to determine winner
def result(comans ,choice):
global result_set
if choice == comans:
print ("Tie")
elif choice == "rock":
if computer == "paper":
print ("You lose")
else :
print("You win")
result_set ='win'
elif choice == "paper":
if computer == "scissors":
print("You lose")
else:
print("You win")
result_set ='win'
elif choice == "scissors":
if computer == "rock":
print("You lose")
else:
print("You win")
result_set ='win'
#Function to get win taly
def wincounter (result,guess,computer):
if result_set == 'win':
win += 1
else:
pass
print (win)
#Main program
counter = 0
win = 0
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
while diffulty != '1' and diffulty != '2' and diffulty != '3':
print ('Invalid input')
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
if diffulty == '1':
print ("You have choose easy")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 3:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
if diffulty == '2':
print ("You have choose medium")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 5:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
if diffulty == '3':
print ("You have choose hard")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 10:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
这是我的代码。它有点凌乱。对不起,因为我还是python的新手。谢谢您的帮助。 跑完之后。
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose scissors.
You win
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose rock.
Tie
Choose rock, paper , or scissors.rock
You choose rock.
Computer choose rock.
Tie
假设在最后一轮结束后显示胜利结果。
答案 0 :(得分:0)
我修复了你的代码。 。
from random import randint
print ("Rock ,Paper,Scissors game.")
#Function to get computer input
def generate():
comlist = ["rock","paper","scissors"]
comans = comlist[randint(-1,2)]
if comans == "rock":
print ("Computer choose rock.")
elif comans == "paper" :
print ("Computer choose paper.")
elif comans == "scissors":
print ("Computer choose scissors.")
return comans
#Function to get user input
def user():
userchoice = input ("Choose rock, paper , or scissors.")
while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors':
print ("Invalid input. Please enter again")
userchoice = input ("Choose rock, paper , or scissors.")
if userchoice == "rock":
print ("You choose rock.")
choice = userchoice
elif userchoice == "paper" :
print ("You choose paper.")
choice = userchoice
else:
userchoice == "scissors"
print ("You choose scissors.")
choice = userchoice
return choice
#Function to determine winner
def result(comans ,choice):
result_set = ''
if choice == comans:
print ("Tie")
elif choice == "rock":
if computer == "paper":
print ("You lose")
else :
print("You win")
result_set ='win'
elif choice == "paper":
if computer == "scissors":
print("You lose")
else:
print("You win")
result_set ='win'
elif choice == "scissors":
if computer == "rock":
print("You lose")
else:
print("You win")
result_set ='win'
wincounter(result_set)
#Function to get win taly
def wincounter (result):
global win
if result == 'win':
win += 1
else:
pass
#print (win)
def print_win_count():
global win
print ('you have win '+ str(win) + ' times')
#Main program
counter = 0
win = 0
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
while diffulty != '1' and diffulty != '2' and diffulty != '3':
print ('Invalid input')
diffulty = input(' Please enter diffulty.( "1" for easy , "2" for medium, "3" for hard)')
if diffulty == '1':
print ("You have choose easy")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 3:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count()
if diffulty == '2':
print ("You have choose medium")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 5:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count()
if diffulty == '3':
print ("You have choose hard")
counter = 1
guess = user()
computer = generate()
result (computer, guess)
while counter < 10:
guess = user()
computer = generate()
result (computer, guess)
counter +=1
print_win_count(0)