python新手,在whiles和ifs方面有问题
我的问题: 现在的问题是,每当我多次进入游戏,它就会忽略它,永远玩下去。它也不会更新分数,即使我有全局变量,并且每次有赢或输的时候我都会添加这些变量 此外,我不想在玩家每次玩游戏时询问他们的姓名(仅当使用“游戏中的分数不足”功能时)。任何建议/帮助都将不胜感激 我的代码如下,以供参考:python新手,在whiles和ifs方面有问题,python,visual-studio-code,Python,Visual Studio Code,我的问题: 现在的问题是,每当我多次进入游戏,它就会忽略它,永远玩下去。它也不会更新分数,即使我有全局变量,并且每次有赢或输的时候我都会添加这些变量 此外,我不想在玩家每次玩游戏时询问他们的姓名(仅当使用“游戏中的分数不足”功能时)。任何建议/帮助都将不胜感激 我的代码如下,以供参考: from random import randint import sys print("Lets play a game!") dotheywantotplay = input(&quo
from random import randint
import sys
print("Lets play a game!")
dotheywantotplay = input("Do you want to play Rock-Paper-Scissors? (y or n) ")
bestOutOf = int(input("Best out of: (Enter a number 1-10) "))
player1score = 0
player2score = 0
computerscore = 0
class PlayWithFriend:
def __init__(self, player1, player2, name1, name2, player1score, player2score):
self.player1 = player1
self.player2 = player2
self.name1 = name1
self.name2 = name2
self.player1score = player1score
self.player2score = player2score
if player1 == player2:
print("Tie!")
elif player1 == "Rock":
if player2 == "Paper":
print("You Win", name2,"!")
print(player2, "covers", player1)
player2score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
else:
print("You Win", name1,"!")
print(player1, "smashes", player2)
player1score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
elif player1 == "Paper":
if player2 == "Scissors":
print("You Win", name2,"!")
print(player2, "cut", player1)
player2score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
else:
print("You Win", name1,"!")
print(player1, "covers", player2)
player1score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
elif player1 == "Scissors":
if player2 == "Rock":
print("You Win", name2,"!")
print(player2, "smashes", player1)
player2score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
else:
print("You Win", name1,"!")
print(player1, "cut", player2)
player1score += 1
print("The score is --> %d: %s vs %d: %s" %(player1score, name1, player2score, name2))
else:
print("That's not a valid play. Check your spelling!")
class PlayWithComputer:
def __init__(self, player, name, player1score, computerscore):
self.player = player
self.player1score = player1score
self.computerscore = computerscore
self.name = name
t = ["Rock", "Paper", "Scissors"]
computer = t[randint(0,2)]
if player == computer:
print("Tie!")
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
computerscore += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
else:
print("You win!", player, "smashes", computer)
player1score += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
computerscore += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
else:
print("You win!", player, "covers", computer)
player1score += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
computerscore += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
else:
print("You win!", player, "cut", computer)
player1score += 1
print("The score is --> %d: %s vs %d: computer" %(player1score, name, player2score))
else:
print("That's not a valid play. Check your spelling!")
if dotheywantotplay == "y":
PlayWith = input("Do you want to play Rock-Paper-Scissors with computer or a friend? (computer, friend, exit) ")
while PlayWith in ('computer', 'friend', 'exit'):
if PlayWith == "friend":
player1 = input('Enter first players name: ')
player2 = input('Enter second players name: ')
while player1score + player2score < bestOutOf:
personchoice1 = input("First player... Rock, Paper or Scissors? ")
personchoice2 = input("Second player... Rock, Paper or Scissors? ")
PlayWithFriend(personchoice1, personchoice2, player1, player2, player1score, player2score)
elif PlayWith == "computer":
player = input('Enter first players name: ')
while player1score + computerscore < bestOutOf:
personchoice = input("Rock, Paper, Scissors? ")
PlayWithComputer(personchoice, player, player1score, computerscore)
elif PlayWith == "exit":
sys.exit()
else:
print("That's not a valid play. Check your spelling!")
else:
print("I am sad now :-(")
sys.exit()
来自随机导入randint
导入系统
打印(“让我们玩游戏吧!”)
dotheywantotplay=input(“您想玩石头剪刀吗?(y或n)”)
bestOutOf=int(输入(“bestOutOf:(输入数字1-10)”)
player1score=0
player2score=0
计算机分数=0
与朋友一起玩:
定义初始化(self、player1、player2、name1、name2、player1score、player2score):
self.player1=player1
self.player2=player2
self.name1=name1
self.name2=name2
self.player1score=player1score
self.player2score=player2score
如果player1==player2:
打印(“领带!”)
elif player1==“摇滚乐”:
如果player2==“纸张”:
打印(“您赢了”,命名2,!”)
打印(播放者2,“封面”,播放者1)
player2score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
其他:
打印(“您赢了”,命名1,!”)
打印(播放者1,“粉碎”,播放者2)
player1score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
elif player1==“纸张”:
如果player2==“剪刀”:
打印(“您赢了”,命名2,!”)
打印(播放者2,“剪切”,播放者1)
player2score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
其他:
打印(“您赢了”,命名1,!”)
打印(播放者1,“封面”,播放者2)
player1score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
elif player1==“剪刀”:
如果player2==“摇滚乐”:
打印(“您赢了”,命名2,!”)
打印(播放者2,“粉碎”,播放者1)
player2score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
其他:
打印(“您赢了”,命名1,!”)
打印(播放者1,“剪切”,播放者2)
player1score+=1
打印(“分数为-->%d:%s vs%d:%s”%(player1score,name1,player2score,name2))
其他:
打印(“这不是一个有效的剧本。请检查您的拼写!”)
使用电脑进行课堂游戏:
定义初始值(自我、玩家、姓名、玩家1核心、计算机分数):
self.player=玩家
self.player1score=player1score
self.computerscore=计算机分数
self.name=名称
t=[“石头”、“布”、“剪刀”]
计算机=t[randint(0,2)]
如果播放器==计算机:
打印(“领带!”)
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
elif player==“摇滚乐”:
如果计算机==“纸张”:
打印(“你输了!”,电脑,“封面”,播放器)
计算机分数+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
其他:
打印(“你赢了!”,玩家,“粉碎”,电脑)
player1score+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
elif播放器==“纸张”:
如果计算机==“剪刀”:
打印(“你输了!”,电脑,“切”,播放器)
计算机分数+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
其他:
打印(“你赢了!”,玩家,“封面”,电脑)
player1score+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
elif player==“剪刀”:
如果计算机==“岩石”:
打印(“你输了…”,计算机,“粉碎”,播放器)
计算机分数+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
其他:
打印(“你赢了!”,玩家,“切”,电脑)
player1score+=1
打印(“分数为-->%d:%s vs%d:computer”%(player1score,name,player2score))
其他:
打印(“这不是一个有效的剧本。请检查您的拼写!”)
如果DotheyWantPlay==“y”:
PlayWith=input(“你想和电脑或朋友玩石头剪刀吗?(电脑,朋友,退出)”)
在(‘计算机’、‘朋友’、‘退出’)中玩时:
如果PlayWith==“朋友”:
player1=input('输入第一个玩家名称:')
player2=input('输入第二个玩家名称:')
而player1score+player2scorewhile PlayWith != "computer" or "friend" or "exit":
if PlayWith == "friend":
player1 = input('Enter first players name: ')
player2 = input('Enter second players name: ')
personchoice1 = input("First player... Rock, Paper or Scissors? ")
personchoice2 = input("Second player... Rock, Paper or Scissors? ")
i = 0
while i in range(0, bestOutOf):
if i >= bestOutOf:
sys.exit()
if bestOutOf >= 1 and bestOutOf <= 10:
PlayWithFriend(personchoice1, personchoice2, player1, player2, player1score, player2score)
i += 1
else:
print("Setting default game to: \"best out of three\"")
PlayWithFriend(personchoice1, personchoice2, player1, player2, player1score, player2score)
i = bestOutOf - 3
if PlayWith == "computer":
i = 0
while i in range(0, bestOutOf - 1):
if i >= bestOutOf:
sys.exit()
else:
player = input('Enter first players name: ')
if bestOutOf >= 1 and bestOutOf <= 10:
personchoice = input("Rock, Paper, Scissors? ")
PlayWithComputer(personchoice, player, player1score, computerscore)
else:
personchoice = input("Rock, Paper, Scissors? ")
print("Setting default game to: \"best out of three\"")
PlayWithComputer(personchoice, player, player1score, computerscore)
if PlayWith == "exit":
sys.exit()
elif PlayWith != "exit" or "computer" or "friend":
print("That's not a valid play. Check your spelling!")
from random import choice
def get_result(choice1, choice2):
return {
('paper', 'rock'): dict(win='player 1', action='covers'),
('rock', 'paper'): dict(win='player 2', action='covers'),
('rock', 'scissors'): dict(win='player 1', action='smashes'),
('scissors', 'rock'): dict(win='player 2', action='smashes'),
('scissors', 'paper'): dict(win='player 1', action='cuts'),
('paper', 'scissors'): dict(win='player 2', action='cuts'),
}[(choice1, choice2)] # index this dictionary
def main():
print('Lets play a game!')
if input('Do you want to play Rock-Paper-Scissors? (y or n) ') != 'y':
quit()
num_games = int(input('Best out of: (Enter a number 1-10) '))
num_games = max(min(num_games, 10), 1) # clamp num_games between 1-10
while True: # infinite loop, we can break out or quit when we need
inp = input('Do you want to play Rock-Paper-Scissors with computer or a friend? (computer, friend, exit) ')
if inp == 'exit':
quit()
play_with_computer = inp == 'computer'
name1 = input('Enter first players name: ')
name2 = 'computer' if play_with_computer else input('Enter second players name: ')
score = {name1: 0, name2: 0}
for game_number in range(num_games):
choice1 = input(f'{name1}... rock, paper or scissors? ')
choice2 = choice(['rock', 'paper', 'scissors']) if play_with_computer else input(f'{name2}... rock, paper or scissors? ')
if choice1 == choice2:
print('Tie!')
continue
result = get_result(choice1, choice2)
win_name = name1 if result['win'] == 'player 1' else name2
lose_name = name2 if result['win'] == 'player 1' else name1
score[win_name] += 1
print(f'{win_name} {result["action"]} {lose_name}')
print(f'Game {game_number+1}, the score is {score[name1]} {name1}, {score[name2]} {name2}')
print(name1 if score[name1] > score[name2] else name2, 'wins!')
if __name__ == '__main__': # this just means if the file is run
main()