2014-12-13 16 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 
相關問題