简体   繁体   中英

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

else near the bottom of the code doesn't work at all. It posts the error "File "main.py", line 35 else: ^ 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 the format of the code you pasted above is exactly how it is in your test, the immediate problem is:

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> 

Judging the intent of the code, what you are looking for is to put the ages.append(e) withing the for like this:

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> 

The syntax error you're getting is because the the line before the else is on the same indentation level as the else is. That unindented statement ended the previous block, so now the else has no if to be associated with.

It's not clear to me exactly what the correct indentation would be. It's either:

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

Or:

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

Note that your very shallow indentation is one of the reasons this issue might not have been obvious to you. Using more spaces per level (four is by far the most common) would make it much clearer. And be consistent! Your current code indents by one space some of the time, and by two spaces other times. That's a recipe for confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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