首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使不满足条件,Python程序也会结束。

即使不满足条件,Python程序也会结束。
EN

Stack Overflow用户
提问于 2022-03-25 10:43:56
回答 1查看 46关注 0票数 0

条件是程序将结束:-if,余额小于或等于零。-if余额大于或等于200。

但问题是,在我输入1或2(正面1,尾部2)之后,它就结束了,您需要再次运行它,余额就不会保存。

这是我的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-25 11:11:40

你搞砸了一些事情,比如,在正确的时间,在什么时候停止循环,采取哪种平衡等等。

如果您想输入一次用户输入,然后检查猜测,下面是代码:

代码语言:javascript
复制
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)

但是,如果您想在每一次机会中接受用户的输入,请使用:

代码语言:javascript
复制
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)

样本输出:(不完整输出,因为它很长)

代码语言:javascript
复制
Congrats you guessed right, You won $9.
Avalilable balanace is : 206
Congrats!! You reached your Target $200.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71615634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档