Python 为什么我的程序中的if部分从来都不是真的?
这是我的第一个程序,这只是代码中不起作用的部分。我不知道怎么了,如果有人能帮助我,我会很高兴的。当您获胜时会出现问题,它不会打印Python 为什么我的程序中的if部分从来都不是真的?,python,Python,这是我的第一个程序,这只是代码中不起作用的部分。我不知道怎么了,如果有人能帮助我,我会很高兴的。当您获胜时会出现问题,它不会打印if部分,而是打印else部分 import re import random def game (): while True: print("Welcome to heads or tails") N = input("""Amount of money to bet : $""") n = int(N
if
部分,而是打印else
部分
import re
import random
def game ():
while True:
print("Welcome to heads or tails")
N = input("""Amount of money to bet :
$""")
n = int(N)
r = range (0,101,10)
if n in r:
print ("Start the game!!" )
T = input ("""How many heads?
""")
t= int(T)
a= range (0,11)
if t in a :
B = input("""How many tails?
""")
b=int(B)
if b in a and b+t== 10:
headsCount = 0
tailsCount = 0
count = 0
while count < 10 :
coin = random.randrange(2)
if coin == 0:
headsCount +=1
print ("Heads")
else:
tailsCount +=1
print ("Tails")
count +=1
score1= str(headsCount)
score2= str(tailsCount)
print (score1 + " times it was heads")
print (score2 + " times it was tails")
#Here is where i think the problem is
if headsCount==t and tailsCount==b:
print("You winn!")
else :
print("Try again, i'm sure that this time you're going to win!")
x = input("""press p to continue or q to exit
""" )
if x== ("q"):
print('godbye, see you soon!')
break
if x == ("p"):
game()
if b in a and b+t< 10:
print ("You have to select more times")
else:
print ("Multiples of 10 only")
game ()
重新导入
随机输入
def游戏():
尽管如此:
打印(“欢迎来到正面或反面”)
N=输入(“下注金额”):
$""")
n=int(n)
r=范围(0101,10)
如果r中为n:
打印(“开始游戏!!”)
T=输入(““”多少头?
""")
t=int(t)
a=范围(0,11)
如果t在a中:
B=输入(““”有多少条尾巴?
""")
b=int(b)
如果a中的b和b+t==10:
人数=0
尾风=0
计数=0
当计数小于10时:
硬币=随机。随机范围(2)
如果硬币=0:
人数+=1
打印(“头”)
其他:
尾流+=1
打印(“尾部”)
计数+=1
分数1=str(总人数)
分数2=str(尾流)
打印(分数1+“打印次数”)
打印(分数为2+“打印次数”)
#我认为问题就在这里
如果车头计数=t,尾箱计数=b:
打印(“你赢了!”)
其他:
打印(“再试一次,我相信这次你会赢的!”)
x=输入(“”)按p继续或按q退出
""" )
如果x==(“q”):
打印(“再见,再见!”)
打破
如果x==(“p”):
游戏()
如果a中的b和b+t<10:
打印(“您必须选择更多次”)
其他:
打印(“仅10的倍数”)
游戏()
t
和b
是整数,score1
和score2
是字符串。字符串永远不会与整数进行比较,因此if
子句永远不会为true
尝试将其更改为:
if headsCount==t and tailsCount==b:
t
和b
是整数,score1
和score2
是字符串。字符串永远不会与整数进行比较,因此if
子句永远不会为true
尝试将其更改为:
if headsCount==t and tailsCount==b:
不是对你的问题的回答,但是要确保你把你的导入语句放在脚本的开头,而不是代码的主体。不是对你的问题的回答,但是要确保你把你的导入语句放在脚本的开头,而不是代码的主体。