Python如何设置一个变量使其值由用户插入?

2024-09-29 23:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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。我该怎么做


Tags: 用户formatifcdrandommathelseque
3条回答

使用input()函数:

CD = int(input())
CD = int(raw_input())

函数的作用是:返回一个字符串到CD,不管你是在控制台中输入int,float,boolean还是string,因为你的工作与算术运算有关,所以我把它转换成float,但是你也可以使用

CD = float(raw_input())

如果您试图输入一个浮点值,但是如果您显式地定义原始输入()函数的类型,或者如果您在控制台中输入字母字符作为输入,那么它将引发一个错误:)

在Python3.x中尝试以下方法:

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

对于python 2.x:

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

相关问题 更多 >

    热门问题