简体   繁体   English

Python-如果输入是字母或数字,则显示不同的输出

[英]Python - display different output if the input is a letter or a number

I want to print the result of the equation in my if statement if the input is a digit and print "any thing" if it is a letter. 如果输入是数字,我想在if语句中打印方程式的结果,如果输入是字母,则要打印"any thing"

I tried this code, but it's not working well. 我尝试了这段代码,但效果不佳。 What is wrong here? 怎么了

while 1:
    print '\tConvert ciliuse to fehrenhit\n'
    temp = input('\nEnter the temp in C \n\t')
    f = ((9/5)*temp +32)
    if temp.isdigit():
        print f
    elif temp == "quit" or temp == "q" :
        break
    elif temp.isalpha() :
        print ' hhhhhhh '

You need to go through your code line by line and think about what type you expect each value to be. 您需要逐行检查代码,并考虑每个值的期望类型 Python does not automatically convert between, for example, strings and integers, like some languages do, so it's important to keep types in mind. Python不会像某些语言一样自动在例如字符串和整数之间进行转换,因此记住类型很重要。

Let's start with this line: 让我们从这一行开始:

temp = input('\nEnter the temp in C \n\t')

If you look at the documentation for input() , input() actually calls eval() on what you type in in Python 2.x (which it looks like you're using). 如果您查看input() 的文档 ,则input()实际上会对您在Python 2.x中input()调用eval() (看起来就像您在使用)。 That means that it treats what you type in there as code to be evaluated, just the same as if you were typing it in the shell. 这意味着它将您在其中键入的内容视为要评估的代码,就像您在外壳中键入内容一样。 So if you type 123 , it will return an int ; 因此,如果您输入123 ,它将返回一个int if you type 'abc' , it will return a str ; 如果您输入'abc' ,它将返回一个str and if you type abc (and you haven't defined a variable abc ), it will give you an error. 如果您键入abc (并且尚未定义变量abc ),它将给您一个错误。

If you want to get what the user types in as a string, you should use raw_input() instead. 如果要获取用户以字符串形式输入的内容,则应使用raw_input()代替。

In the next line: 在下一行:

f = ((9/5)*temp +32)

it looks like you're expecting temp to be a number. 您似乎希望temp是一个数字。 But this doesn't make sense. 但这没有道理。 This line gets executed no matter what temp is, and you're expecting both strings containing digits and strings containing letters as input. 无论temp是多少,都将执行此行,并且您期望包含数字的字符串和包含字母的字符串都作为输入。 This line shouldn't go here. 这条线不应该去这里。

Continuing on: 继续:

if temp.isdigit():

isdigit() is a string method, so here you're expecting temp to be a string. isdigit()是一个字符串方法,因此在这里您希望temp是一个字符串。 This is actually what it should be. 这实际上是应该的。

This branch of the if statement is where your equation should go, but for it to work, you will first have to convert temp to an integer, like this: if语句的该分支是您的方程式应运行的位置,但是要使其起作用,首先必须将temp转换为整数,如下所示:

c = int(temp)

Also, to get your calculation to work out right, you should make the fraction you're multiplying by a floating-point number: 另外,为了使计算正确,您应该将要乘以的分数乘以浮点数:

f = ((9/5.0)*c +32)

The rest of your code should be okay if you make the changes above. 如果进行了上述更改,那么其余的代码应该没问题。

A couple of things first - always use raw_input for user input instead of input . 首先有raw_input -始终使用raw_input代替用户input input will evaluate code, which is potentially dangerous. input将评估代码,这有潜在的危险。

while 1:
    print "\tConvert ciliuse to fehrenhit\n"
    temp = raw_input("\nEnter the temp in C \n\t")

    if temp in ("quit", "q"):
        break

    try:
        f = ((9.0 / 5.0) * float(temp) + 32)
    except ValueError:
        print "anything"

Instead of using isalpha to check if input is invalid, use a catch clause for ValueError , which is thrown when a non-numerical value is used. 代替使用isalpha来检查输入是否无效,请使用ValueError的catch子句,该子句在使用非数字值时抛出。

Why isn't it working? 为什么不起作用? Are you getting an error of any kind? 您遇到任何错误吗?

Straight away I can see one problem though. 马上我就能看到一个问题。 You are doing the calculation before you verify it as a number. 您需要先进行计算,然后再将其验证为数字。 Move the calculation to inside the if temp.isdigit() . 将计算移至if temp.isdigit()内部。

Take a look at this for some examples: 来看一些示例:

http://wiki.python.org/moin/Powerful%20Python%20One-Liners http://wiki.python.org/moin/Powerful%20Python%20One-Liners

OK, this works. 好,这有效。 Only problem is when you quit, you get dumped out of the interpreter. 唯一的问题是当您退出时,您会被转出解释器。

while 1: import sys; 而1:导入sys; temp=raw_input('\\nEnter the temp in C \\n\\t'); temp = raw_input('\\ n在C中输入温度\\ n \\ t'); temp.isdigit() and sys.stdout.write('%lf' %((9./5)*float(temp)+32)) or temp=='q' and sys.exit(0) or sys.stdout.write(temp) temp.isdigit()和sys.stdout.write('%lf'%((9./5)*float(temp)+32))或temp =='q'和sys.exit(0)或sys。 stdout.write(temp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM