01/10/2018, 11:52

Hỏi đáp về Python

Mọi người ơi, em không biết làm sao để lưu tất cả các input của người nhập rồi sao đó in ra tất cả input thành một list. Input của em yêu cầu người dùng nhập tên và số. Và sau đó em muốn in tất cả các số cạnh bên tên. Em xin lỗi nếu em giải thích không rõ ràng.

Sau đây là câu hỏi bài tập của em:
Write a program that asks the user to enter the amount that he or she has budgeted for
a month. A loop should then prompt the user to enter each of his or her expenses for the
month and keep a running total. When the loop finishes, the program should display the
amount that the user is over or under budget.

In each repetition through the loop, the program should prompt the user for name of the expense and the amount of the expense. The program should print the name of the expense and the amount of the expense. keep a running total of all expenses entered. When the loop finishes, the program should display the total
amount that the user entered and indicate if the total amount is over or under budget.

The program should print the amount of the expense for each item next to item name

The program should display the amount budgeted

The program should display the amount actually spent

The program should display the amount that the user is over or under budge

indicate if the total amount is over or under budget.

Còn đây là bài làm của em:

userbudget = int(input("Please enter how much you have budgeted for the month: "));
moreExpenses = "y"
totalExpenses = 0

while moreExpenses == "y":
    userExpenses = str(input("Enter the name of expense: "))
    Expenses = int(input("Enter an expense: "))
    totalExpenses += Expenses
    moreExpenses = input( "Do you have more expenses?: Type y for yes, any key for no" )
for i in userExpenses:
    for n in Expenses:
        print(i,n)
    


if totalExpenses > userbudget:
    print( "You were over your budget of",userbudget,"by",totalExpenses - userbudget )
elif userbudget > totalExpenses:
    print( "You were under your budget of",userbudget,"by",userbudget - totalExpenses )
else:
    print("You used exactly your budget of", userbudget,".")

Em cảm thấy bế tắc khi không thể in ra tất cả các Expenses mà người dùng nhập vào ở cạnh bên tên của Expense. Mọi người có thể giải thích dùm em được không ạ? em xin cám ơn.

Nguyễn Duy Hùng viết 14:03 ngày 01/10/2018

bạn phải dùng một list để lưu lại rồi mới in được chứ.

phuonghuynh viết 13:54 ngày 01/10/2018

À mình tìm được đáp án rồi bạn. Thanks bạn. Mình lưu input dưới dạng list rồi print kết quả ra.

Nguyễn Duy Hùng viết 13:58 ngày 01/10/2018

When the loop finishes, the program should display the total
amount that the user entered and indicate if the total amount is over or under budget.

Cái dòng này nó nói là khi hết vòng lặp rồi thì không cần in ra các khoản chi tiêu nữa đâu nên bỏ cái vòng for đi, chỉ cần in ra tổng khoảng chi tiêu lớn hay bé hơn ngân sách thôi

phuonghuynh viết 14:06 ngày 01/10/2018

Tại thầy của mình ổng đòi chứ mình đâu muốn làm chi cho nhiều haha.
Yêu cầu sau khi loop xong thì in ra tên chi tiêu cùng với số tiền chi tiêu ở cạnh. Nên mình mới tạo các tên của các giá trị chi tiêu, giá trị chi tiêu thành 2 list rồi in ra.

Bài liên quan
0