Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user input and output
#1
Hello Gurus,

I need some help with this(Remember, ">>" indicates user input);

I want to be able to let the user input their monthly expenses as an example below.
Welcome to the family financial analyzer.
How much does the family spend on groceries per month?
>> 1200.50
How much does the family spend on dining out per month?
>> 500.00

Here are your family finances analyzed.
Your family spends $1,700.50 on food per month

When I try this in python this is what I have done;
(1)
print("How much does the family spend on groceries per month?")
groceries_monthly = eval(input("How much does the family spend on groceries per month?: "))
print("How much does the family spend on dining out per month?")

and I have tried this too;
(2)
print("How much does the family spend on groceries per month?")
print("How much does the family spend on dining out per month?")


however, in number (1) it prints the statement twice and I only want it to print once and then allow the user to enter the amount and print the next question. in (2) it prints the statement one after the other and it doesn't allow the user to input the amount until both statements finish running.

Once that is done then print what the family spends on food for the month.

Thanks in advance for your help. Smile
aka2d7
Reply
#2
First, don't use eval? It is dangerous. The input can be a Python code which deletes all of your data/files/file systems. The code will be evaluated and executed.

print('How much does the family spend on groceries per month?')

groceries = input('>> ')
Same with the diners.

costs = int(groceries) + int(diners)

Print(costs)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
RESTART: /Users/aka2d7/Documents/Step 6 Arithmetic.py 
How much does the family spend on groceries per month?
>> 1200.50
How much does the family spend on dining out per month?
>>500
Traceback (most recent call last):
File "/Users/aka2d7/Documents/Step 6 Arithmetic.py", line 10, in <module>
costs = int(groceries_monthly) + int(diningout_monthly)
ValueError: invalid literal for int() with base 10: '1200.50'
>>>
Reply
#4
1200.50 can't be cast from string to integer, only to float type.
Reply
#5
Hello Gurus,

I'm not sure how to complete this code;

Your family spends $1,700.50 on food per month

so if I used this;
costs = float(groceries_monthly) + float(diningout_monthly)
print(costs)

it gives me the 1700.50 but not in the sentence like above.
and I have tried these also;

print("Your family spents\ costs\ on food per month?")

print("Your family spents" + "costs" + "on food per month?")

but it's not giving me what I'm looking for. Please advise! Smile

Again Thanks for all of your support and help!

aka2d7
Reply
#6
I don't know what exactly you want printed, but something like this maybe for a start:

print("Your family spends $" + str(costs) + " on food per month?")
Or a bit more elegant:
print("Your family spends ${0:.2f} on food per month?".format(costs))
See details about input/output formatting here: https://docs.python.org/3/tutorial/inputoutput.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interactive process for input and output maiya 1 2,301 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  How to revert back to a previous line from user input Sharkenn64u 2 3,186 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
Question [SOLVED] Same input different output antarling 2 1,556 Oct-25-2024, 11:28 PM
Last Post: antarling
  User input with while loops chizzy101010 2 12,610 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 3,711 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  output provide the filename along with the input file processed. arjunaram 1 2,031 Apr-13-2023, 08:15 PM
Last Post: menator01
  restrict user input to numerical values MCL169 2 2,564 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 2,911 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 3,132 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 3,268 Dec-11-2022, 07:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020