我对编码还很陌生,所以请多多包涵。我必须在python中创建一种老虎机,显示0-5的数字,如果玩家获得了特定的数字序列,他们将赢得特定的金额。我已经创建了所有代码来对玩家进行打分并打印出来,但是我无法循环播放该代码块。我想从开始循环(如果want_play ==“ p”和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指令而不是循环(while / for)如果满足条件,则if指令仅执行一次,否则,它将在else分支上执行一次。如果要循环,则应考虑使用while指令或for指令:
while指令将在满足while条件的情况下执行其下的所有内容(为true)。
同时(want_play ==“ p”和player_score> = 0):打印“你好”
这将打印问候,只要wants_play == "p" and player_score >= 0
。它不会停止打印问候,直到其中之一被更改或调用break指令为止。
While( wants_play == "p" and player_score >= 0):
print "hello"
break
将只打印一次问候,因为调用了break关键字,这将终止循环的执行
While( wants_play == "p" and player_score >= 0):
print "hello"
player_score-=1
只要player_score >= 0
就会打个招呼;每次执行循环时,减去1的player_score最终将变为<0,即while的执行将结束时
因为您使用了and关键字,所以两个条件都必须满足。如果其中之一不再为真,则while的执行将停止
wants_play == "p" and player_score >= 0