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
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  output provide the filename along with the input file processed. arjunaram 1 903 Apr-13-2023, 08:15 PM
Last Post: menator01
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,025 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 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,031 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 1,837 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,138 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,829 May-10-2022, 02:44 AM
Last Post: alisha17
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R

Forum Jump:

User Panel Messages

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