2014-12-13 18 views
0
a = (math.ceil(6*random.random()))  
b = (math.ceil(6*random.random())) 
c = (math.ceil(6*random.random()))  
CD = (10)  
e = (a+b+c)  
print ("a = {0}" .format (a))  
print ("b = {0}" .format (b))  
print ("c = {0}" .format (c))  
print ("a+b+c = {0}" .format (e))  
if a+b+c > CD:  
    print ("Maior que {0}" .format(CD))  
else: 
    print ("Menor que {0}" .format(CD)) 

我想允许用户更改变量CD。我怎样才能做到这一点?Python如何设置一个变量来使用户插入其值?

回答

2

尝试此蟒3.X:

CD = float(input("Enter a number: ")) 

对于Python 2.x的:

CD = float(raw_input("Enter a number: ")) 
+2

或'浮动(的raw_input( “输入一个数字:”))'如果在Python 2.7 – aruisdante 2014-12-13 15:38:19

+0

THX,把它添加到anwser。 – elyase 2014-12-13 15:40:26

0
CD = int(raw_input()) 

的raw_input的()函数返回一个字符串到CD问题上没有如果在控制台中输入一个int,float,boolean或字符串,因为你的工作与算术运算有关,所以我将它转换为浮点数,但是确定你也可以使用

CD = float(raw_input()) 

,如果你想进入一个浮点值,但是,如果你明确的raw_input定义()函数的类型,或者这时如果输入字母字符在控制台输入,然后它会产生一个错误:)

0

使用input()功能:

CD = int(input()) 
0

这应该是有帮助的:

如果您正在使用Python 3.X

CD = float(input("Enter your number: ")) # or any other data type 

如果您正在使用Python 2.x的

CD = float(raw_input("Enter a number: ")) # or any other data type 
相关问题