我对Python很陌生,但写了一个简单的Rock,Paper,Scissors游戏。
它的代码如下:
from __future__ import print_function
import random
name = raw_input("Hi, I'm PyVa. What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input(' Would you like to play Rock, Paper, Scissors?: ')
yesorno = str(yesorno)
if yesorno == str('Yes'):
choices = ['Rock', 'Paper', 'Scissors']
print('Ok')
your_choice = raw_input('Choose Rock, Paper or Scissors: ')
print('My turn...')
my_choice = random.choice(choices)
print('I choose,', my_choice)
if your_choice == my_choice:
print('We both choose the same, it is a draw.')
elif your_choice == 'Rock' and my_choice == 'Paper':
print('I win!')
elif your_choice == 'Scissors' and my_choice == 'Paper':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Rock':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Scissors':
print('I win!')
elif your_choice == 'Rock' and my_choice == 'Scissors':
print('You win!')
elif your_choice == 'Scissors' and my_choice == 'Rock':
print('I win!')
again = raw_input('Would you like to play again?:')
#this is where I would like to loop to the start depending on input.
else:
print('Ok, maybe we can play later, bye.')
现在我可以想象这甚至不是优雅的代码,并且必须有更精确的方法来编写我想要做的事情。 (请给出你有时间的任何指示。)
我主要担心的是如何在每场比赛结束后正确插入循环以返回第二个区块即ie。基于用户的raw_input,开始或游戏,或简单地结束。
非常感谢您提供任何帮助。
答案 0 :(得分:2)
你去了:
from __future__ import print_function
import random
name = raw_input("Hi, I'm PyVa. What is your name: ")
print('Hi',name, end='.')
n = 0
while True:
if 'y' in raw_input('Would you like to play Rock, Paper, Scissors?: ').lower() or n > 0:
my_choice = random.choice(['rock', 'paper', 'scissors'])
print('Ok')
your_choice = raw_input('Choose Rock, Paper or Scissors: ').lower()
print('My turn...')
print('I choose,', my_choice)
if your_choice == my_choice:
print('We both choose the same, it is a draw.')
elif your_choice in ['rock', 'scissors'] and my_choice == 'paper':
print('I win!')
elif your_choice == 'paper' and my_choice in ['rock', 'scissors']:
print('You win!')
elif your_choice == 'rock' and my_choice == 'scissors':
print('You win!')
elif your_choice == 'scissors' and my_choice == 'rock':
print('I win!')
n += 1
if not 'y' in raw_input('Would you like to play again?:').lower():
break
#this is where I would like to loop to the start depending on input.
它现在也不区分大小写,当游戏退出时你会感到烦恼,或者因为你不小心忘记以大写字母开头写你的答案而输了。我还使用'in'
简化了一点答案 1 :(得分:0)
我会在那里插入循环:
# ...
yesorno = raw_input(' Would you like to play Rock, Paper, Scissors?: ')
while yesorno == 'Yes':
choices = ['Rock', 'Paper', 'Scissors']
# ...
yesorno = raw_input(' Would you like to play Rock, Paper, Scissors?: ')
print('Ok, maybe we can play later, bye.')
答案 2 :(得分:0)
while True:
<your code>
if not 'y' in raw_input('Would you like to play again?: ').lower():
break;
答案 3 :(得分:0)
我要做的是删除不必要的变量,而是将实际游戏放在一个函数中。这允许您在不询问玩家是否想要重播的情况下调用它一次,然后进行一个while循环,用户可以决定是否要继续播放。我希望这有帮助。我自己无法测试这个,因为我使用的是Python 2.7,但除了一些潜在的语法差异外,代码应该可以工作。 我建议这样的事情:
from __future__ import print_function
import random
name = raw_input("Hi, I'm PyVa. What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input(' Would you like to play Rock, Paper, Scissors?: ')
if yesorno == 'y':
play()
def play():
choices = ['Rock', 'Paper', 'Scissors']
print('Ok')
your_choice = raw_input('Choose Rock, Paper or Scissors: ')
print('My turn...')
my_choice = random.choice(choices)
print('I choose,', my_choice)
if your_choice == my_choice:
print('We both choose the same, it is a draw.')
elif your_choice == 'Rock' and my_choice == 'Paper':
print('I win!')
elif your_choice == 'Scissors' and my_choice == 'Paper':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Rock':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Scissors':
print('I win!')
elif your_choice == 'Rock' and my_choice == 'Scissors':
print('You win!')
elif your_choice == 'Scissors' and my_choice == 'Rock':
print('I win!')
while True:
play_again = raw_input('Play Again?(Y/N)')
if play_again.lower() == 'y':
play()
elif play_again.lower() == 'n':
print('Ok, maybe we can play later, bye.')
break
else:
print('That isn\'t a valid option...')
答案 4 :(得分:-1)
我没有时间清理它,但这是一种可以修复特定问题的方法。你将游戏包裹在一个始终为真的while循环中,并在需要时退出。
from __future__ import print_function
import random
name = raw_input("Hi, I'm PyVa. What is your name: ")
print('Hi',name, end='.')
yesorno = raw_input(' Would you like to play Rock, Paper, Scissors?: ')
while(1):
yesorno = str(yesorno)
if yesorno == str('Yes'):
choices = ['Rock', 'Paper', 'Scissors']
print('Ok')
your_choice = raw_input('Choose Rock, Paper or Scissors: ')
print('My turn...')
my_choice = random.choice(choices)
print('I choose,', my_choice)
if your_choice == my_choice:
print('We both choose the same, it is a draw.')
elif your_choice == 'Rock' and my_choice == 'Paper':
print('I win!')
elif your_choice == 'Scissors' and my_choice == 'Paper':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Rock':
print('You win!')
elif your_choice == 'Paper' and my_choice == 'Scissors':
print('I win!')
elif your_choice == 'Rock' and my_choice == 'Scissors':
print('You win!')
elif your_choice == 'Scissors' and my_choice == 'Rock':
print('I win!')
again = raw_input('Would you like to play again? [y/n]:')
if again is "y":
next
else:
print('Thanks for playing!\n')
exit()
else:
print('Ok, maybe we can play later, bye.')