if语句和条件的问题

时间:2014-10-21 15:52:48

标签: python python-3.x

我编写的代码如下所示:

import random

diceOne = random.randint(1,6) 
diceTwo = random.randint(1,6)


print ('Playing Craps:') 
print('You have rolled a', diceOne ,'and a', diceTwo ) 
print ('for a total of', diceOne + diceTwo )


if diceOne == 3 and diceTwo == 3:
    print('This is called a Hard Six')

可以看出,我已经设法使条件在if声明下工作。但问题是我无法弄清楚怎么说所有六个(除了两个三分之一)的变化都会被标记为“Easy Six”,例如5和1,2和4等

对不起,我忘了提到我一般都是Python和编码的初学者,所以请尽量使解释相当简单。

2 个答案:

答案 0 :(得分:1)

我建议你在if语句中使用嵌套的if语句

if dice1+dice2==6:
     if dice1==3 and dice2==3:
          print('This is called a Hard Six')
     else:
          print('This is called Easy Six')

答案 1 :(得分:0)

正如jonrsharpe所说,你可以添加一个elif条件,这表明你的骰子不等于3(因此你的代码不会输入你的if语句)但是它们的总数仍然等于6:

if diceOne == 3 and diceTwo == 3 :
    print('This is called a Hard Six')

elif (diceOne + diceTwo == 6) :
    print('This is called an Easy Six')

我可以在这里为Python初学者推荐一个非常好的教程。它有时看起来有点慢,但它非常适合对基础知识感到满意。 http://www.codecademy.com/en/tracks/python

相关问题