python - 使用布尔值重复 while 循环
问题描述
抱歉,如果这被认为是垃圾邮件,我遇到的另一个问题是这个 while 循环无条件运行,我不知道如何修复它。
代码:
while anotherNum == True:
playerNum = random.randint(1,10)
total = total + playerNum
print("")
print("Your number is ", str(playerNum) + ".")
print("You have " + str(total) + " in total.")
print("")
again = input("Roll another number? <Y or N> ")
print("")
if again == "Y":
anotherNum == True
else:
anotherNum == False
break
#game finished now
print("Computer got", pcNum)
print("You got", total)
#checking for winner
while anotherNum == False:
if (total <= 13) and (total > pcNum):
print("You,", name, "have won!")
elif (pcNum <= 13) and (pcNum > total):
print("The computer has bested you,", name + "!")
else:
if (total == pcNum) and (total <= 13):
print("Draw...")
elif (pcNum > 13) and (total <= 13):
print("You,", name + " have won!")
else:
print("Both of you have lost. Wow...")
输出:
Your number is 2.
You have 2 in total.
Roll another number? <Y or N> n
Your number is 3.
You have 3 in total.
Roll another number? <Y or N> N
Your number is 3.
You have 3 in total.
Roll another number? <Y or N> N
Your number is 9.
You have 9 in total.
而不是去#game完成现在评论的区域,while循环重复这个过程,同时只忽略total = total + playerNum命令。
解决方案
你没有改变价值
if again == "Y":
anotherNum == True
else:
anotherNum == False
break
您应该使用 1 个等号,因为它是一个作业
if again == "Y":
anotherNum = True
else:
anotherNum = False
break
推荐阅读
- sas - 如何在 LOOP 中打开 SAS 库中的所有表?
- javascript - 在参数 deleteObject aws 中缺少必需的键“键”
- postgresql - Liquibase 和 Postgresql 集成
- laravel - Laravel:仅清除选定的缓存
- c++ - 类构造函数中的数据结构错误和 C++ 中类的不可访问性
- android-notifications - 如何访问 Android (TV) 上的推送通知计数器?
- robotframework - 从进度条 RobotFramework 读取值
- class - 关于 Raku 中 EVAL 中的哈希绑定的问题
- php - 未捕获的错误:在 null 上调用成员函数 generate_cart_id()
- swift - 如何将函数内部的值传递给Swift中的类中的全局变量?