Skip to main content
 首页 » 编程设计

python之如何让用户输入一个十进制数以及一个整数

2025年05月04日10shanyou

如何让用户输入他们想要的任何类型的数字?这是我尝试过的:

a=0 
def main(): 
    x = int(input("Input a number in Celsius to convert it to Fahrenheit")) 
    f = (x*1.8)+(32) 
    print(f, "degrees Fahrenheit") 
 
a=a+1 
while a > 0: 
    main() 
 
main() 

请您参考如下方法:

只需使用这个:

x = float(input('Input a number in celcius to convert it to fahrenheit')) 

然后用x进行所需的计算。

阅读 python 中的基本类型 here .

无论如何,问题中给出的缩进是完全错误的。您的代码甚至不应该运行。而且您运行 main() 的逻辑也很荒谬。它可能是这样的:

a=0 
 
def main(): 
    x = float(input("Input a number in celcius to convert it to fahrenheit")) 
    f = (x*1.8)+(32) 
    print(f, "degrees fahrenheit") 
 
while a < 10: # for taking 10 inputs and converting 
    main() 
    a += 1