条件是程序将结束:-if,余额小于或等于零。-if余额大于或等于200。
但问题是,在我输入1或2(正面1,尾部2)之后,它就结束了,您需要再次运行它,余额就不会保存。
这是我的代码:
import random
def Guess_Check(guess,balance): #function for guess check
coin_flip = int( random.choice([1,2]))
if coin_flip == 1:
print("It's heads!!")
else:
print("It's tail!")
if guess == coin_flip: #if guess is correct
print("Congrats you guessed right, You won $9.")
balance = balance+9
else: #if guess is wrong
print("Sorry your guess was wrong, You loss $10.")
balance = balance-10
print("Avalilable balanace is :", balance)
return (balance)
def Balance_Check(Balance): #Balance Check
if Balance <= 10: #we can't play the game if balance is below $10
print("Sorry!! You run out of money.")
return(1)
if Balance >=200:
print("Congrats!! You reached your Target $200.")
return(1)
balance = 100 #beginning amount
while True:
bal = Balance_Check(balance) #check the available balance
if bal==1:
break
print("Guess heads by entering 1 or tails by entering 2 for this coin flip.") #Asking the player to guess
guess = int(input())
Balance = Guess_Check(guess,balance)发布于 2022-03-25 11:11:40
你搞砸了一些事情,比如,在正确的时间,在什么时候停止循环,采取哪种平衡等等。
如果您想输入一次用户输入,然后检查猜测,下面是代码:
import random
def Guess_Check(guess,balance): #function for guess check
coin_flip = random.choice([1,2])
if coin_flip == 1:
print("It's heads!!")
else:
print("It's tail!")
if guess == coin_flip: #if guess is correct
print("Congrats you guessed right, You won $9.")
balance = balance+9
else: #if guess is wrong
print("Sorry your guess was wrong, You loss $10.")
balance = balance-10
print("Avalilable balanace is :", balance)
return (balance)
def Balance_Check(Balance): #Balance Check
if Balance <= 10: #we can't play the game if balance is below $10
print("Sorry!! You run out of money.")
return 0
elif Balance >=200:
print("Congrats!! You reached your Target $200.")
return 1
balance = 100 #beginning amount
print("Guess heads by entering 1 or tails by entering 2 for this coin flip.") #Asking the player to guess
guess = int(input())
while Balance_Check(balance) not in [0,1]: # You need to run the loop till balance becomes 0 or 200
balance = Guess_Check(guess,balance)但是,如果您想在每一次机会中接受用户的输入,请使用:
import random
def Guess_Check(guess,balance): #function for guess check
coin_flip = random.choice([1,2])
if coin_flip == 1:
print("It's heads!!")
else:
print("It's tail!")
if guess == coin_flip: #if guess is correct
print("Congrats you guessed right, You won $9.")
balance = balance+9
else: #if guess is wrong
print("Sorry your guess was wrong, You loss $10.")
balance = balance-10
print("Avalilable balanace is :", balance)
return (balance)
def Balance_Check(Balance): #Balance Check
if Balance <= 10: #we can't play the game if balance is below $10
print("Sorry!! You run out of money.")
return 0
elif Balance >=200:
print("Congrats!! You reached your Target $200.")
return 1
balance = 100 #beginning amount
while Balance_Check(balance) not in [0,1]: # You need to run the loop till balance becomes 0 or 200
guess = int(input("Guess heads by entering 1 or tails by entering 2 for this coin flip."))
balance = Guess_Check(guess,balance)样本输出:(不完整输出,因为它很长)
Congrats you guessed right, You won $9.
Avalilable balanace is : 206
Congrats!! You reached your Target $200.https://stackoverflow.com/questions/71615634
复制相似问题