我的编程教授已委托我创建一个程序,该程序要求用户输入要输入的正整数(包括零)。除正整数以外的任何内容都将停止输入,并计算/显示数字的总和,平均值和计数。
我要解决的主要问题是返回要在程序中传递的多个正整数。我似乎找不到我需要的东西。
inputPostiveInteger()
Pass in nothing
Ask the user for a positive integer
Return the integer is the user inputs one
Return -1 if the user does not input a positive integer
他显示程序正常工作的方式就是示例-
Enter a positive integer, anything else to quit: 1
Enter a positive integer, anything else to quit: 5
Enter a positive integer, anything else to quit: 10
Enter a positive integer, anything else to quit: cat
Sum 16
Average 5.3
Total numbers 3
我尝试了多种方法try / except和while not子句来使输入部分正确无误,但似乎无法将我的脑筋包住。
我了解如何计算/显示平均值,计数,总和的公式-但似乎无法在多个公式中找到经过审查的整数条目。
例如尝试输入
def inputPositiveInteger():
try:
userInt = int(input("Enter a positive integer, anything else to quit: ")) > -1
except ValueError:
return -1
def main():
total = 0
count = 0
posInt = inputPositiveInteger()
while posInt != -1:
total += posInt
count += 1
main()
total = 0
userInt = int(input('Enter a positive integer, anything else to quit: '))
while userNumber > -1:
total += userInt
userInt = int(input('Enter a positive integer, anything else to quit: '))
答案 0 :(得分:3)
我尝试使我的解决方案有点类似于您的代码:
def inputPositiveInteger():
try:
userInt = int(input("Enter a positive integer, anything else to quit: "))
except ValueError:
return -1
if userInt < 0:
return -1
return userInt
def main():
total = 0
count = 0
posInt = inputPositiveInteger()
while posInt != -1:
total += posInt
count += 1
posInt = inputPositiveInteger()
if count == 0:
print("Sum:", None)
print("Average:", None)
print("Total numbers:", None)
else:
print("Sum:", total)
print("Average:", total/count)
print("Total numbers:", count)
main()
您可能已经也可能没有介绍Arrays a.k.a.我认为将要走的列表,在这里也没有尝试,除非必要:
def inputPositiveInteger():
userInt = input("Enter a positive integer, anything else to quit: ")
# str.isnumeric checks that a string only contains numbers and isn't empty
if not userInt.isnumeric():
return -1
return int(userInt)
def main():
posInt = inputPositiveInteger()
nums = []
while posInt != -1:
nums.append(posInt)
posInt = inputPositiveInteger()
if nums:
print("Sum:", sum(nums))
print("Average:", sum(nums)/len(nums))
print("Total numbers:", len(nums))
else:
print("Sum:", None)
print("Average:", None)
print("Total numbers:", None)
main()
希望这对您有帮助,祝您上课好运。
答案 1 :(得分:1)
尝试一下:
def finish(summ,count):
print('Sum',summ)
print('Count',count)
print('Avg',round(summ/count,5))
summ = 0
count = 0
while True:
try:
userInt = int(input('Enter a positive integer, anything else to quit: '))
if userInt >= 0:
summ += userInt
count += 1
else:
finish(summ,count)
break
except:
finish(summ,count)
break
答案 2 :(得分:0)
感谢您的建议以缩小输入范围,我最终将其作为最终提交的代码。
def calcAverage(total, count):
return round(float(total) / float(count), 2)
# prompt user to enter posInt
def inputPositiveInteger():
userInt = input("Enter a positive integer, anything else to quit: ")
# return input if digit, else return -1
if not userInt.isdigit():
return -1
return int(userInt)
def main():
# call function
posInt = inputPositiveInteger()
nums = []
while posInt != -1:
nums.append(posInt)
posInt = inputPositiveInteger()
if nums:
print("Sum", sum(nums))
print("Average", calcAverage(sum(nums), len(nums)))
print("Total numbers", len(nums))
main()