简体   繁体   English

为什么我的python代码不执行,而是给我一个“无”错误,而只执行其中的一部分?

[英]Why is my python code not executing, but instead giving me a “None” error and only executing part of it?

I made a program to solve a quadratic equation by inputting a, b, and c and then it would return the solutions, or if it had any and it would return that it had no real solutions if not. 我编写了一个程序来解决二次方程,方法是输入a,b和c,然后它将返回解,或者如果有解,则返回没有实解。

It gave me a couple of syntax errors that I fixed, and I tried to import the time module to delay it, but it still doesn't work. 它给了我一些固定的语法错误,我尝试导入时间模块以延迟它,但是它仍然不起作用。 I think it is because I didn't give it like a keyword to start running or something. 我认为这是因为我没有给它像开始运行的关键字之类的东西。

import math

print('What quadratic do you want to solve?')        

a = input(print("a= "))
b = input(print("b= "))
c = input(print("c= "))

discriminant = b**2 - 4*a*c

if discriminant < 0:
    print('There are no solutions to this quadratic!')

x_1 = (-b + math.sqrt(discriminant))/2*a
x_2 = (-b - math.sqrt(discriminant))/2*a

if x_1 == x_2:
    print('The solution is ' + x_1)

print('The solutions are ' + x_1 + ' and ' + x_2)

I expected the output to ask the values of a, b, and c and then give me a result, but it just gave me 我希望输出会询问a,b和c的值,然后给我一个结果,但这只是给了我

What quadratic do you want to solve?
a= 
None

It is asking you for the value of a , but the code is a bit weird so the display gets messed up. 它询问您a的值,但是代码有点奇怪,所以显示混乱了。 The program will not proceed until you enter the value for a ; 该计划将无法继续,直到您输入值a ; it is waiting patiently for you to do that. 它正在耐心地等待您这样做。

Normally, input function is expecting a single argument, which is the prompt it would display. 通常, input函数需要一个参数,即它将显示的提示。 print function is used for printing, and returns None . print功能用于打印,并返回None What happens is, print("a=") prints that, and returns None ; 发生的是, print("a=")打印出来,并返回None then you get input(None) which understands None to be the prompt to be printed, and you get this sort of messed up output. 然后您得到input(None) ,它理解None是要打印的提示,并且您会得到这种混乱的输出。 The code should be either 代码应为

print("a=", end="")
a = input()

or just 要不就

a = input("a=")

There's more errors in your code once you get past this; 一旦超过了这个,代码中就会有更多的错误。 eg b**2 - 4*a*c is calculated, but never stored anywhere, and thus ends up discarded. 例如,计算b**2 - 4*a*c ,但从未将其存储在任何地方,因此最终被丢弃。 Meanwhile, discriminant variable was never defined, and you'll hear Python complain about it. 同时, discriminant变量从未定义过,您将听到Python抱怨它。 Also, something / 2*a means (something / 2) * a , but correct formula needs something / (2 * a) (which you can also write as something / 2 / a ). 同样, something / 2*a表示(something / 2) * a ,但正确的公式需要something / (2 * a) (您也可以写为something / 2 / a )。

EDIT with more details from comments, as I missed them (and thanks to TrebledJ for being more observant): since Python3 input returns strings, you will need to cast them to floats (using float(...) function) for them to be usable in calculation; 编辑时会从注释中获取更多详细信息,因为我错过了它们(并且感谢TrebledJ更加谨慎):由于Python3 input返回字符串,因此您需要将它们强制转换为浮点数(使用float(...)函数)可用于计算; and similarly, the results, being numbers, will not cleanly concatenate with strings using + (you would have to either cast them to strings using str(...) function, or use .format string method or % formatting operator to get everything to mesh). 同样,结果(即数字)将不会使用+干净地与字符串连接(您必须使用str(...)函数将其转换为字符串,或者使用.format字符串方法或%格式运算符将所有内容转换为字符串啮合)。

Python's print statement always returns None . Python的print语句始终返回None So in input(print("a= ")) , you're essentially evaluating input(None) . 因此,在input(print("a= ")) ,您实质上是在评估input(None) Another problem in your code is in the line 您的代码中的另一个问题是

print('The solutions are ' + x_1 + ' and ' + x_2)

You can't concatenate int and str objects so you must turn the number into a str object before printing it. 您无法连接intstr对象,因此必须在打印之前将数字转换为str对象。

print('The solutions are ' + str(x_1) + ' and ' + str(x_2))
import math

print('What quadratic do you want to solve?')        

a = input("a= ")
b = input("b= ")
c = input("c= ")

discriminant = b**2 - 4*a*c

if discriminant < 0:
    print('There are no solutions to this quadratic!')

x_1 = (-b + math.sqrt(discriminant))/2*a
x_2 = (-b - math.sqrt(discriminant))/2*a

if x_1 == x_2:
    print('The solution is ' + x_1)

print('The solutions are ' + str(x_1) + ' and ' + str(x_2))

Why your program is returning None is perfectly explained in the above two answers. 上面两个答案很好地解释了为什么程序返回None原因。 So, you can also write the print and input statement in a single line as 因此,您也可以将print和input语句写成一行

a = int(input(f"a = " ))

And before in the input statement, you need to specify int or float which will be the type of a, b and c. 在输入语句之前,您需要指定int或float,它们将是a,b和c的类型。 Without it, they will be of type str which will again raise a TypeError in declaring the discriminant variable. 没有它,它们将是str类型,这将再次在声明判别变量时TypeError

And finally in the if statement, after checking if the discriminant is < 0 and printing the statement you need to quit the program using exit() function. 最后,在if语句中,检查判别式是否小于0并打印该语句后,您需要使用exit()函数退出程序。 Else the rest of the program will be executed regardless and we don't want that. 否则,程序的其余部分将被执行,我们不希望这样做。

Also, check the above two answers, they have rightly pointed out other small mistakes in the code. 另外,检查上面的两个答案,它们正确地指出了代码中的其他小错误。

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

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