在Python中if,elif和else的缩进错误 [英] Indentation error with if, elif and else in Python

查看:1047
本文介绍了在Python中if,elif和else的缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello编码社区!

我9岁,热爱Python编程,这是我第一次发帖 - 祝我好运!

我刚写完一个简单的计算器 - 在Python中 - 我主要是集中精力编写实际的代码(通常我花了很长时间打印长行告诉用户该做什么 - 我应该这样做结束!)。我也试图用尽可能少的帮助来做这件事,看看我是否能独立(靠我自己)代码。但无论如何,我一直收到这个缩进错误:



  Traceback(最近一次调用最后一次):
File python,第9行
z =输入(请输入你的下一个数字:)
TabError:缩进中标签和空格的使用不一致



请快点帮忙,否则我无法测试我的程序,看它是否有效!

我会很感激我能得到任何帮助。

这个是我的全部代码:



  一个简单的计算器 - 我这样做完全没有帮助和非打印,如打印,所以这里去! 
def 计算器( x,y,z):
operation = input( a = Add,s = Subtract,m =乘以,d =除以
如果 operation == 一个
x =输入( 第一个数字:
y = input( 第二个数字:
other = input( 任何其他数字?
elif 其他== y
z = input( 请输入您的下一个号码:
return x + y + z
else
return x + y

如果操作== s
x = input( 第一个数字:
y = input( 第二个数字:
return x - y
如果操作== m
x =输入( 第一个数字:
y = input( 第二个数字:
return x * y
如果 operation == d
x = input( 第一个数字:
y = input( 第二个数字:
返回 x / y

print (计算器)

计算器( x,y)





我尝试过:



我试过把

  lang =Python>   

中的> elif

语句,同样使用

  else  

语句。

解决方案

elif和else块是检查other的输入,所以它应该是一个if else块并且像other变量一样缩进。

您可以使用elif作为其他操作值,因为它们属于同一个流链。

最后一点,它没有任何意义使用函数你的情况,所以用do打印替换所有的return语句。


试试这个

  def 计算器(x,y,z):
operation = input( a = Add,s = Subtract,m = Multiply,d =除以
如果 operation == a
x = input( 第一个数字:
y = input( 第二个数字:
other = input( 还有其他任何数字吗?
if other == y 这是一个新的if块
z = input( 请输入您的下一个号码:
return x + y + z
其他
返回 x + y

elif operation == s 这继续if operation ...上面
x = input( 第一个数字:
y = input( 第二个数字:
return x - y
elif operation == m 这样做
x =输入( 第一个数字:
y =输入( 第二个数字:
返回 x * y
elif operation == d 这样做
x =输入( 第一个数字:
y = input( 第二个数字:
return x / y

print (计算器)

计算器(x ,y)



您还宣布计算器作为三个参数( x,y ,z ),但是你要求用户输入它们。而且你在代码底部的电话只发送 x y



但是,我不得不说,对于一个9岁的孩子来说这是一个非常好的代码,保留它up。


顺便说一句,上面的所有解决方案都不起作用。您不能声明参数调用将获得用户输入的函数。因为调用该函数你必须为它传递参数,所以它不会抛出错误。



其次,如果需要字符串输入,则调用raw_input函数。如果需要整数,则调用输入函数。这是该计算器的工作代码,我写它是最容易阅读的。玩得开心,祝你好运如果你需要编程帮助,请查看我的文章。我为那些刚学会专业的人写信。我需要一个星期的时间来完成一门课程,但如果你阅读我的文章,你可以找到适合你的编程语言。



这是工作程序。使用一个内置错误检查循环来向您展示它是如何完成的。

  def 计算器() :

operation = raw_input( a = Add,s = Subtract,m = Multiply, d =除以:
(操作 操作 s 操作 m 操作 d 操作 q):
print 输入错误,请重试。输入q退出
operation = raw_input( a = Add,s = Subtract ,m =乘以,d =除以:
如果操作 q
return ;


如果操作 a
x = input( < span class =code-string>第一个数字:)
y = input( 第二个数字:
other = raw_input( 还有其他任何数字吗?
如果其他== y
z = input( 请输入您的下一个号码:
return x + y + z
else
return x + y

如果操作 s
x =输入( 第一个数字:
y = input( 第二个数字:
return x - y
如果操作== m
x = input( 第一个数字:
y = input( Second number:
return x * y
如果操作 d
x = input( 第一个数字:
y = input( 第二个数字:
return x / y


a =计算器()
print (a)


Hello coding community!
I am 9 years old and love programming in Python and this is my first time posting - so wish me luck!
I have just written a simple calculator - in Python - and I mainly struggle with concentration on writing the actual code (normally I spend ages printing long lines telling the user what to do - I should do that the end!). I am also trying to do this with as little help as possible to see if I can independently (on my own) code. But anyway, I keep on receiving this indentation error:

Traceback (most recent call last):
File "python", line 9
z = input("Please input your next number:")
TabError: inconsistent use of tabs and spaces in indentation


Please help quickly otherwise, I can't test my program to see if it works!
I would be grateful for any help I can get.
This is my whole code:

#A simple calculator - I am doing this entirely without help and nonsanse such as printing, so here goes!
def calculator(x, y, z):
	operation = input("a = Add, s = Subtract, m = Multiply, d = Divide")
	if operation == "a":
		x = input("First number:")
		y = input("Second number:")
		other = input("Any other numbers?")
	elif other == "y":
    	z = input("Please input your next number:")
    	return x + y +  z
	else:
	    return x + y
	    
	if operation == "s":
		x = input("First number:")
		y = input("Second number:")
		return x - y
	if operation == "m":
		x = input("First number:")
		y = input("Second number:")
		return x * y
	if operation == "d":
		x = input("First number:")
		y = input("Second number:")
		return x / y
		
print (calculator)

calculator(x, y)



What I have tried:

I have tried putting the

elif

statement inside the

if

, and same with the

else

statement.

解决方案

The elif and else block is to check the input for "other", so it should be an if else block and indented like the "other" variable.
You could have use elif for other "operation" values as they belong to the same chain of flow.
One last point, it serves no purpose to use a function in your case, so replace all the return statements with print with do.


Try this

def calculator(x, y, z):
    operation = input("a = Add, s = Subtract, m = Multiply, d = Divide")
    if operation == "a":
        x = input("First number:")
        y = input("Second number:")
        other = input("Any other numbers?")
        if other == "y":     # this is a new if block
            z = input("Please input your next number:")
            return x + y +  z
        else:
            return x + y
        
    elif operation == "s":     # this continues the "if operation ..." above
        x = input("First number:")
        y = input("Second number:")
        return x - y
    elif operation == "m":     # so does this
        x = input("First number:")
        y = input("Second number:")
        return x * y
    elif operation == "d":     # so does this
        x = input("First number:")
        y = input("Second number:")
        return x / y
    
    print (calculator)

calculator(x, y)


You have also declared calculator as taking three parameters (x,y,z), but then you ask the user to type them in. Also your call at the bottom of the code only sends x and y.

However, I have to say, for a 9 year old that is some pretty good code, keep it up.


By the way all the solution above does not work. You can't declare arguments call for function that is going to get user input. Because to call that function you have to pass argument for it so it doesn't throw error.

Secondly, if you need a string input you call raw_input function. If you need integer number, you call the input function. This is the working code of that calculator and I wrote it as easiest to read as possible. Have fun and good luck If you need programming help, look for my articles. I write for people that just learning to the very expert one. It take me a week to go through one subject but if you read my article you can find out which programming language which suit you.

Here is the working program. With one loop of built in error check to show you how it is done.

def calculator():
 
    operation = raw_input("a = Add, s = Subtract, m = Multiply, d = Divide:")
    while (operation is not "a" and operation is not "s" and operation is not "m" and operation is not "d" and operation is not "q"):
        print("Incorrect input, please try again. Type q to quit" )
        operation = raw_input("a = Add, s = Subtract, m = Multiply, d = Divide:")
        if operation is "q":
            return;

    
    if operation is "a":
        x = input("First number:")
        y = input("Second number:")
        other = raw_input("Any other numbers?")  
        if other == "y":
            z = input("Please input your next number:")
            return x + y + z
        else:
            return x + y
 
    if operation is "s":
        x = input("First number:")
        y = input("Second number:")
        return x - y
    if operation == "m":
        x = input("First number:")
        y = input("Second number:")
        return x * y
    if operation is "d":
        x = input("First number:")
        y = input("Second number:")
        return x / y
 
 
a = calculator()
print(a)


这篇关于在Python中if,elif和else的缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆