亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用Python查找n個數字的平均值

用Python查找n個數字的平均值

catspeake 2021-03-29 12:23:01
我正在嘗試創建一個程序,該程序可以找到輸入的n個數字的平均值,但是我無法使遞歸正常工作。該程序可以工作,但在我希望的時候不會退出while語句。print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average findercounter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added togetherfirst_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''') #takes input of yes or no to see whether user wants to find average of numberswhile first_question == "yes" :    ent_num = int(input("Enter your number here:"))    sum_of_numbers = sum_of_numbers + ent_num    counter = counter + 1    second_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')     while second_question == "yes" :         ent_num = int(input("Enter your next number here: "))         sum_of_numbers = sum_of_numbers + ent_num        counter = counter + 1    else :    print("Your average is " + str(sum_of_numbers/counter))有人可以幫我解決嗎?我不能使用諸如try或eval之類的功能,也不能使用它的所有真正基本的東西,例如我上課的第三天
查看完整描述

3 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

您只需要一個循環即可工作。只需問您一個問題,獲取輸入,然后循環即可。當您輸入no時,循環將退出并計算您的平均值。


print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average finder


counter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator 

sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added together


first_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''') #takes input of yes or no to see whether user wants to find average of numbers


while first_question == "yes" :

    ent_num = int(input("Enter your number here:"))

    sum_of_numbers = sum_of_numbers + ent_num

    counter = counter + 1

    first_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')


print("Your average is " + str(sum_of_numbers/counter))


查看完整回答
反對 回復 2021-04-06
?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

您的代碼足夠簡潔,無法對其進行注釋。畢竟是蟒蛇!


這是一個簡短的工作版本:


print("This program 'is' designed to find the average of n numbers you input\n")


first_question = 'Would you like to enter a number? (type "yes" if you do)\n\n'

second_question = 'Would you like to enter another number after this? (type "yes" if you do)\n'


sum_of_numbers = 0

counter = 0

if input(first_question) == "yes" :

    sum_of_numbers += int(input("Enter your number here:"))

    counter += 1

    while input(second_question) == "yes" :

        sum_of_numbers += int(input("Enter your next number here: "))

        counter += 1

    print("Your average is " + str(sum_of_numbers/counter))


查看完整回答
反對 回復 2021-04-06
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

first_question永遠不會更改,并且您在完成第二個練習后還應該進行數學運算。由于您將輸出分配給second_question,因此first_question始終保持“是”。由于您永遠不會再簡單地詢問first_question:


print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average finder


counter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator 

sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added together


first_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''')


 #takes input of yes or no to see whether user wants to find average of numbers.


if first_question == "yes" :

    ent_num = int(input("Enter your number here:"))

    sum_of_numbers = sum_of_numbers + ent_num

    counter = counter + 1

    second_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')

    while second_question == "yes" :

         ent_num = int(input("Enter your next number here: "))

         sum_of_numbers = sum_of_numbers + ent_num

         counter = counter + 1

    print("Your average is " + str(sum_of_numbers/counter))

else :

    print("Well if you're just going to answer no off the bat why did you bother running me\n");


查看完整回答
反對 回復 2021-04-06
  • 3 回答
  • 0 關注
  • 283 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號