繁体   English   中英

为什么代码中显示的else不能在这种情况下工作?

[英]why doesn't the else shown in code not work in this situation?

代码底部附近的其他部分根本不起作用。 它发布错误“File”main.py“,第35行:^ SyntaxError:invalid syntax”

print("Numbers Inputted:", ages) #provides the user input into a list Form
x = input("Would You like to Delete the fifth number on your list? [yes/no]") #input for whethor or not the user wants to delete the fifth number on the list 
if x == 'yes':
 del ages[4] #deletes fourth line of code because i is +1
else: #if another input is inputted 
 print("Ages Given After Deletion or after remove option has been given", ages) #prints ages after the user has choosen to change the ages/numbers or not

y = input("Would you like to add a number onto your list? [yes/no]") #question for addition of a number
if y == 'yes': 
 for i in range (1): 
   e = float(input("enter the age that you would like to add:  ".format(i+1)))
ages.append(e)
else: 
 print("Calculating Results...") #print statement to provide seperation between calculations and user input 
print("min: ", min(ages)) #Prints min of ages using command
print("max: ", max(ages)) #prints max of ages using command  
print("sum: ", sum(ages)) #prints sum of ages using command 
print("average: ", sum(ages)/len(ages)) #prints average of given ages by diving sum by total inputs given 
s_ages = sorted(ages) #sorts list from highest to lowest
print('Ages From Lowest To highest', s_ages) #prints sorted list
counter=collections.Counter(ages) #gives frequency of user inputs
print("Frequency: [number inputted by user is on the left, while the amount of times that number was inputted in total is on the right...]", counter.most_common(5))
#provides specificity of output and prints frequency as well  

如果您上面粘贴的代码格式与测试中的格式完全相同,那么当前的问题是:

if y == 'yes': 
   for i in range (1): 
      e = float(input("enter the age that you would like to add:  ".format(i+1)))
ages.append(e) #<--- this line indicates to Python that your `if` is all there is.
else: #<--- this is considered as an `else` without an `if`. That is the problem
   <rest of the code> 

判断代码的意图,你要找的是将ages.append(e)for这样:

if y == 'yes': 
   for i in range (1): 
      e = float(input("enter the age that you would like to add:  ".format(i+1)))
      ages.append(e) #<--- this line indicates to Python that your `if` is all there is.
else: #<--- this is considered as an `else` without an `if`. That is the problem
   <rest of the code> 

你得到的语法错误是因为之前该行else在同一缩进级别的else的。 那个未缩进的语句结束了前一个块,所以现在else没有与if关联。

我不清楚确切的缩进是什么。 它是:

if y == 'yes': 
 for i in range (1): 
   e = float(input(...))
 ages.append(e)  # indent this the same as the for?
else:
 ...

要么:

if y == 'yes': 
 for i in range (1): 
   e = float(input(...))
   ages.append(e)  # or further?
else:
 ...

请注意,您的非常浅的缩进是此问题可能对您不明显的原因之一。 每级使用更多空格(最常见的是四个)会使它更加清晰。 并保持一致! 您当前的代码在某些时候缩进一个空格,而在其他时间缩进两个空格。 这是混乱的一个秘诀。

暂无
暂无

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

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