簡體   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