我是相当新的编码,所以忍受我。我必须在python中创建一个老虎机,显示数字0 - 5,如果玩家得到一个特定的数字序列,他们赢得了一个特定的金额。我已经创建了所有的代码,给玩家打分,并打印出来,但我不能循环的块.我想开始循环从(if wants_play == "p" and player_score >= 0:)这里是我的代码。
import random
player_score = 1
round_score = 0
wants_play = input("Press 'P' to play or 'Q' to quit. ")
if wants_play == "p" and player_score >= 0:
print("you have " + str(player_score) + " pound")
num1 = random.randint(0, 5)
num2 = random.randint(0, 5)
num3 = random.randint(0, 5)
print("you got numbers:", num1, num2, num3)
round_score = 0
player_score = 0.2
if num1 == num2 or num1 == num3 or num2 == num3:
round_score = 0.5
if num1 == num2 and num1 == num3 and num2 == num3:
round_score = 1
if num1 == 0 and num2 == 0 and num3 == 0:
round_score = 5
if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
round_score = -1
if num1 == 1 and num2 == 1 and num3 == 1:
round_score = 0
player_score = 0
print("you won ", round_score, " pounds!")
player_score += round_score
if player_score < 0:
player_score = 0
else:
print("Goodbye")
exit()'''
你不能循环,因为你使用了if指令而不是循环(whilefor)if指令只执行一次,如果条件满足,否则,它就会进入else分支,也是一次,如果你想循环,你应该考虑使用while指令或for指令。
如果你想循环,你应该考虑使用 while 指令或 for 指令:只要 while 指令的条件满足(为真), while 指令就会执行它下面的所有内容。
While( wants_play == "p" and player_score >= 0): print "hello" (你好)
这将打印出hello,只要 wants_play == "p" and player_score >= 0
......它不会停止打印hello,直到其中一个变化或调用break指令。
While( wants_play == "p" and player_score >= 0):
print "hello"
break
只打印一次hello,因为调用了break关键字,这将终止循环的执行。
While( wants_play == "p" and player_score >= 0):
print "hello"
player_score-=1
将打印出hello,只要 player_score >= 0
每次执行循环时,都会从player_score中减去1,最终变成<0,这时,while的执行就会结束。
因为你用了 和 如果其中一个条件不再为真,那么while的执行将停止。
wants_play == "p" and player_score >= 0
我不太明白你想用玩家分数和回合分数来做什么,你想如何实现它们,但我修改了答案,所以它每次都会问你是否要继续。
import random
player_score = 1
round_score = 0
wants_play = input("Press 'P' to play or 'Q' to quit. ")
while(wants_play=="P" or wants_play == "p"):
print("you have " + str(player_score) + " pound")
num1 = random.randint(0, 5)
num2 = random.randint(0, 5)
num3 = random.randint(0, 5)
print("you got numbers:", num1, num2, num3)
round_score = 0
player_score = 0.2
if num1 == num2 or num1 == num3 or num2 == num3:
round_score = 0.5
if num1 == num2 and num1 == num3 and num2 == num3:
round_score = 1
if num1 == 0 and num2 == 0 and num3 == 0:
round_score = 5
if num1 == 1 and num2 == 1 or num1 == 1 and num3 ==1 or num2 == 1 and num3 == 1:
round_score = -1
if num1 == 1 and num2 == 1 and num3 == 1:
round_score = 0
player_score = 0
print("you won ", round_score, " pounds!")
player_score += round_score
if player_score < 0:
player_score = 0
wants_play = input("Press 'P' to play or 'Q' to quit. ")
print("Goodbye")
exit()