根本不起作用。它发布错误“文件“ main.py”,第35行 其他: ^ SyntaxError:语法无效”
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
答案 0 :(得分:0)
如果您上面粘贴的代码格式与测试中的格式完全相同,则直接的问题是:
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>
答案 1 :(得分:0)
您得到的语法错误是因为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:
...
请注意,您的缩进很浅是此问题可能对您而言不明显的原因之一。每级使用更多的空间(到目前为止,最常见的是四个)将使其更加清晰。并保持一致!您当前的代码有时缩进一个空格,而有时缩进两个空格。那是造成混乱的秘诀。