试图在Python中制作游戏..虽然我似乎无法获得某些代码的工作?!这让我疯了!
非常感谢任何帮助!
import random
die1 = random.randint(1,10)
die2 = random.randint(1,10)
die3 = random.randint(1,10)
if die1 > die2:
print ('Die 1:', die2,' Die 2:', die1,)
else:
print ('Die 1:', die1,' Die 2:', die2,)
if die1 == die2 or die2 == die1:
print('\nEven-steven!')
else:
print('\nNot the same, let\'s play!')
print ('\nDie 3:', die3,)
if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2:
print ('\n*** You win! ***')
elif die1 > die2:
if die3 < die2 or die3 > die1:
print('\n*** Sorry - You lose! ***')
elif die2 > die1:
if die3 < die1 or die3 > die2:
print('\n*** Sorry - You lose! ***')
**elif die3 == die1 or die3 == die2:
print ('\n*** You hit the post - You lose double your bet! ***')**
print ('\nThanks for playing!')
它位于第二个if语句结构中,无论出于何种原因,如果die1或die2与die3相同,它就不会打印出“你点击帖子......”它只是结束了!
谢谢!
答案 0 :(得分:1)
您的诊断不正确。如果die3
等于die1
或die2
, 的最后一个语句将匹配:
>>> die1, die2 = 1, 2
>>> die3 = die1
>>> die3 == die1 or die3 == die2
True
>>> die3 = die2
>>> die3 == die1 or die3 == die2
True
但是,如果die1
大于die2
您的第一个 elif
匹配,但您与die3
的比较存在缺陷:
>>> die1, die2 = 2, 1
>>> die3 = die1
>>> die3 < die2 or die3 > die1
False
打印和没有。您需要清理该案例的逻辑。
因为你只有3种不同的结果;赢得,失败和击中帖子,你可以真正简化整个事情:
if die1 < die3 < die2:
print('\n*** You win! ***')
elif die3 == die1 or die3 == die2:
print('\n*** You hit the post - You lose double your bet! ***')
else:
print('\n*** Sorry - You lose! ***')
请注意,我使用chained comparisons来简化第一个if
表达式。您还确保die1
低于die2
,因此对die2 < die3 < die1
的测试始终将为False
。
另一个注意事项:==
应该是可传递的,因此die1 == die2 or die2 == die1
是多余的。您可以将其简化为die1 == die2
。
简化整个计划:
import random
die1 = random.randint(1,10)
die2 = random.randint(1,10)
if die1 > die2:
die1, die2 = die2, die1
print ('Die 1:', die1,' Die 2:', die2,)
if die1 == die2:
print('Even-steven!')
else:
print("Not the same, let's play!")
die3 = random.randint(1,10)
print ('Die 3:', die3)
if die1 < die3 < die2:
print('\n*** You win! ***')
elif die3 == die1 or die3 == die2:
print('\n*** You hit the post - You lose double your bet! ***')
else:
print('\n*** Sorry - You lose! ***')
print ('Thanks for playing!')
答案 1 :(得分:1)
不要让它疯狂,保持冷静,并使用pdb
。
if die3 in (die1, die2):
import pdb; pdb.set_trace()
if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2:
print ('\n*** You win! ***')
elif die1 > die2:
if die3 < die2 or die3 > die1:
print('\n*** Sorry - You lose! ***')
elif die2 > die1:
if die3 < die1 or die3 > die2:
print('\n*** Sorry - You lose! ***')
**elif die3 == die1 or die3 == die2:
print ('\n*** You hit the post - You lose double your bet! ***')**
print ('\nThanks for playing!')
现在运行它,当它运行时,开始调试(使用n
进行下一步,检查每个条件的值等)。
答案 2 :(得分:1)
通过简化代码,调试起来会容易得多。只有3个条件win
,lose
或lose (double)
,因此不需要6个(嵌套)条件。如果die1
您的程序输出和变量保持有意义,也可以切换die2
和die1 > die2
。这是更健全的:
import random
die1 = random.randint(1,10)
die2 = random.randint(1,10)
if die1 > die2:
die1, die2 = die2, die1
print('Die 1:',die1,'Die 2:',die2)
if die1 == die2:
print('Even-steven!')
else:
print("Not the same, let's play!")
die3 = random.randint(1,10)
print('Die 3:',die3)
if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2:
print ('You win!')
elif die3 == die1 or die3 == die2:
print ('You hit the post - You lose double your bet!')
else:
print('Sorry - You lose!')
print('Thanks for playing!')
答案 3 :(得分:0)
最终if语句的elif
仅作为第3个条件的内部if
语句的替代解决方案的一部分进行处理。
以下内容应该可以解决您的问题。请注意更改
if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2:
print ('\n*** You win! ***')
elif die1 > die2 and (die3 < die2 or die3 > die1):
print('\n*** Sorry - You lose! ***')
elif die2 > die1 and (die3 < die1 or die3 > die2):
print('\n*** Sorry - You lose! ***')
elif (die3 == die1) or (die3 == die2):
print ('\n*** You hit the post - You lose double your bet! ***')
享受。 :)
答案 4 :(得分:0)
这个怎么样?
if die3 == die1 or die3 == die2:
print ('\n*** You hit the post - You lose double your bet! ***')
else:
if die3 > die1 and die3 < die2 or die3 < die1 and die3 > die2:
print ('\n*** You win! ***')
elif die1 > die2:
if die3 < die2 or die3 > die1:
print('\n*** Sorry - You lose! ***')
elif die2 > die1:
if die3 < die1 or die3 > die2:
print('\n*** Sorry - You lose! ***')