运行时期间的Python不会将输入的数字视为int或float

时间:2016-09-14 18:15:10

标签: python input

我是初学者,可能会觉得这是一个愚蠢的问题。

我正在尝试创建一个简单的程序,从Celsius到F *的温度转换器。

需要:

  1. '退出'停止程序
  2. 用于提供正确结果的int值
  3. 问题:用户输入的任何文本值都是通过try expect error和while循环来处理的,它要求他们再次输入int值,但是第二次用户输入正确的值(即int),系统认为它是一个字符串,循环永远不会结束。

    这是我的计划:

    # Program to convert Celsius to Fahrenheit
    
    import time
    
    while True:
    
        userinput= input(" 1 Enter the temperature in celsius: "
                     "Enter Quit if you want to close the converter")
    
        if userinput.lower()=='quit':
            print('Closing the application. Thanks for using the converter')
            break
    
        try:
            userinput= float(userinput)
    
        except ValueError:
            print('You cannot enter text, please use numbers')
            while userinput.lower()!='quit':
    
                if type(userinput)!=str:
                    userinput = float(userinput)
                    print(type(userinput))
    
                    break
                else:
                    while type(userinput)==str:
    
                        userinput = (input(" 2. Oops Enter the temperature in celsius: "
                                  "Enter Quit if you want to close the converter"))
                        print(type(userinput))
                        ## my problem is here even if enter a integer 2 as userinput. i  am getting the type userinput as str. Can someone help me
    
        type(userinput)
        userinput=float(userinput)
        f=userinput*9.0/5+32
        print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)
    

5 个答案:

答案 0 :(得分:1)

打印后的代码('您无法输入文字,请使用数字') 不是必需的。由于此代码,一旦输入错误的输入,它就会陷入无限循环

由于在while循环中询问输入,因此虽然输入错误,但会询问用户输入。

代码(检查评论):

while True:
#Get input
userinput= input(" 1 Enter the temperature in celsius: "
                 "  Enter Quit if you want to close the converter")
#Check user wants to quit
if userinput.lower()=='quit':
    print('Closing the application. Thanks for using the converter')
    break

try:
    #Check type of userinput
    print "Type is =",type(userinput)
    #Check user input is number
    userinput= float(userinput)
    f=userinput*9.0/5+32
    print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)

except ValueError:
    #If user input is not number, then print error message.
    #After this, again user input will be asked due to while loop.
    print('You cannot enter text, please use numbers')

<强>输出:

  C:\Users\dinesh_pundkar\Desktop>python c.py
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"57"
Type is = <type 'str'>
('Your input was:', 57.0, '\n\n', 'The corresponding Fareinheit is', 134.6, '\n\
n\n', '-------------------------------------------------------------------------
--')
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"asd"
Type is = <type 'str'>
You cannot enter text, please use numbers
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"34"
Type is = <type 'str'>
('Your input was:', 34.0, '\n\n', 'The corresponding Fareinheit is', 93.2, '\n\n
\n', '--------------------------------------------------------------------------
-')
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"quit"
Closing the application. Thanks for using the converter

C:\Users\dinesh_pundkar\Desktop>

此外,如果您使用的是Python 2,则使用raw_input而不是input。对于Python 3输入工作。

答案 1 :(得分:0)

我不确定我是否正确理解了问题,但通常最好使用raw_input代替input以防万一。

答案 2 :(得分:0)

你在这里使用三个嵌套的while循环没有明显的resons。当用户输入错误的值时,他将转移到下一个永不结束的while循环。这是一个快速而肮脏的解决方案。

# Program to convert Celsius to Fahrenheit

import time

while True:

    userinput= input(" 1 Enter the temperature in celsius: "
                 "Enter Quit if you want to close the converter")

    try:
        userinput= float(userinput)
        type(userinput)  
        userinput=float(userinput)
        f=userinput*9.0/5+32
        print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)


    except ValueError:
        print('You cannot enter text, please use numbers')        

        if userinput.lower()=='quit':
            print('Closing the application. Thanks for using the converter')
            break

答案 3 :(得分:0)

问题在于:

if type(userinput)!=str:

由于input返回一个字符串,userinput始终是一个字符串:因此,上面的代码片段始终为false。 您应该尝试将其解析为像this answer

中的浮点数

答案 4 :(得分:0)

在1输入中,你投射它,在第二个输入中,你从不投射。

我也注意到,你从未真正为该循环实现“退出”功能。这应该使您的代码更容易阅读。

def checkToClose(value):
  if (value.lower() == 'quit')
    quit()

while True:
  userinput= input(" 1 Enter the temperature in celsius: "
             "Enter Quit if you want to close the converter")

  checkToClose(userinput)

  try:
    userinput= float(userinput)
  catch:
    continue

  f = userinput * 9.0 / 5 + 32
  print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)