import random
computer= 0 #Computer Score
player= 0 #Player Score
print("COP 1000 ")
print("Let's play a game of Chicken!")
print("Your score so far is", player)
r= random.randint(1,8) # this only gets called once, so r is always one value
print("Roll or Quit(r or q)")
你的代码有很多错误。这只起一次作用,因为它不在一个循环中。
改进后的代码:from random import randint
computer, player, q, r = 0, 0, 'q', 'r' # multiple assignment
print('COP 1000') # q and r are initialized to avoid user error, see the bottom description
print("Let's play a game of Chicken!")
player_input = '' # this has to be initialized for the loop
while player_input != 'q':
player_input = raw_input("Roll or quit ('r' or 'q')")
if player_input == 'r':
roll = randint(1, 8)
print('Your roll is ' + str(roll))
# Whatever other code you want
# I'm not sure how you are calculating computer/player score, so you can add that in here
while循环在其下执行所有操作(缩进),直到语句变为false。因此,如果播放器输入q,它将停止循环,并转到程序的下一部分。见:Python Loops --- Tutorials Point
Python 3的挑剔之处在于缺少raw_input。使用input,用户输入的任何内容都将被计算为Python代码。因此,用户必须输入“q”或“r”。但是,避免用户错误(如果播放器只输入q或r,不带引号)的方法是用这些值初始化这些变量。