import random
print('Ви граєте у гру \'Камінь, ножниці, папір\' !')
choices = ['Камінь' , 'Папір' , 'Ножниці']
user_choice = input('Виберіть : Камінь, Ножниці або Папір : \nВаш вибір : ')
computer_choice = random.choice(choices)
print('Комп\'ютер вибрав : ' + computer_choice)
computer_score = 0
user_score = 0
game = False
while game == False:
if user_choice == 'Камінь' and computer_choice == 'Папір':
computer_score += 1
print('Ви програли !')
elif user_choice == 'Камінь' and computer_choice == 'Ножниці':
user_score += 1
print('Ви перемогли !')
elif user_choice == 'Папір' and computer_choice == 'Ножниці':
computer_score += 1
print('Ви програли !')
elif user_choice == 'Папір' and computer_choice == 'Камінь':
user_score += 1
print('Ви перемогли !')
elif user_choice == 'Ножниці' and computer_choice == 'Камінь':
computer_score += 1
print('Ви програли !')
elif user_choice == 'Ножниці' and computer_choice == 'Папір':
user_score += 1
print('Ви перемогли !')
elif user_choice == computer_choice:
print('Нічия !')
else:
print('Неправильне введення. Перевірте написання слова.')
break
print('Користувач : ' + str(user_score)+ ' |----| ' + 'Комп\'ютер : ' + str(computer_score))
所以我有一个问题,我刚刚编写了程序“ Rock Paper Scissors”(我知道它很简单,你甚至可能嘲笑我,但是我只是刚开始),我不知道如何播放它几次。我运行它选择其中一项,得到一个分数,程序关闭。可能如何循环?
答案 0 :(得分:0)
我可能会使用递归,但是您需要添加一种情况,使递归应该达到最高,否则最终将耗尽内存。
{
"version": "2.0.0",
"tasks": [
{
"version": "0.1.0",
"command": "chrome",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"args": ["localhost\\${workspaceRootFolderName}\\${fileBasename}"]
}
]
}
答案 1 :(得分:0)
您可以使用以下结构(将其翻译为英文):
import random
choices = ['Rock' , 'Paper' , 'Scissors']
computer_score = 0
user_score = 0
game = True
while game:
user_choice = input('Choose : Rock, Scissors, or Paper : \nYour choice : ')
computer_choice = random.choice(choices)
print('Computer chose : ' + computer_choice)
if user_choice == 'Rock' and computer_choice == 'Paper':
computer_score += 1
print('You lost !')
elif user_choice == 'Rock' and computer_choice == 'Scissors':
user_score += 1
print('You won !')
# Other choices...
game = (input('Play again? (y/n)\n') == 'y')
print('User : ' + str(user_score)+ ' |----| ' + 'Computer : ' + str(computer_score))
game
现在默认为True
,如果用户确定他们不想玩并在给定游戏后输入n
,则可以更改。他们通过输入y
来继续游戏。现在,用户和计算机的选择将在while循环内移动(以使每个游戏具有一组选择)。一旦用户决定不再玩游戏,分数就会累积并显示在结尾(尽管这只是您希望游戏如何工作的问题)。
请注意,您需要从while循环中删除break
语句(假设OP中的缩进是错误的,并且break
是while
的一部分)。在您选择的第一个if
和检查用户是否希望继续玩游戏之间的原始代码中,仅有的其他部分应该是其他选择(包括未知选择的else
)。