Python Forum
Any way to get raw_input to work in my function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Any way to get raw_input to work in my function?
#1
Hi again! Been playing around with functions and decided to make my own and wanted to make a program that stated food waste in pounds with the data coming from the user but I'm having trouble getting my formatters to be seen as numbers from the users and not a string as it states. Here's the code I wrote.
def food_waste(cold_food, hot_food):
   print "Based on the data given, your total Cold Waste is %d pounds." % cold_food
   print "Data also shows that Hot Waste accumulated to %d pounds." % hot_food
   print "The total amount of food wasted today was %d pounds." % hot_food + cold_food

food_waste(raw_input("Place Cold Food Waste In Pounds Here. > "), raw_input("Place Hot Food Waste In Pounds Here. > "))
This is as far as I got before I was reciving an error. Any help would be appreciated :)

Thanks,
Pythonerous
Reply
#2
Quote:This is as far as I got before I was reciving an error.
What error? An indentation error? A type error?
http://python-forum.io/misc.php?action=help&hid=20
Recommended Tutorials:
Reply
#3
Sorry, I should have put the error beforehand.
Error:
File "ex19.py", line 3, in food_waste TypeError: %d format: a number is required, not str
Reply
#4
Quote:%d


You are using the string expression which requires you to input the correct type of value in. You can change it to %s or you can use string formatting
http://python-forum.io/Thread-Basic-stri...xpressions
Recommended Tutorials:
Reply
#5
I got it to work! Here's my new code for anyone who might have come across a similar problem.

 def food_waste(cold_food, hot_food):
    print "Based on the data given, your total Cold Waste is %d pounds." %cold_food
    print "Data also shows that Hot Waste accumulated to %d pounds." %hot_food
    print "The total amount of food wasted today was %d pounds." %(hot_food + cold_food)


pounds_of_cold = int(raw_input("Enter > "))
pounds_of_hot = int(raw_input("Enter > "))

food_waste(pounds_of_cold, pounds_of_hot)

#Program to calculate food waste, It works! 

Sorry, I thought I was using my script tags correctly... I was trying to delete the post and repost correctly but I see no way to...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 302 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  I dont know why my function won't work? MehHz2526 3 1,198 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  time function does not work tester_V 4 3,041 Oct-17-2021, 05:48 PM
Last Post: tester_V
  write new function or change the old one to work "smartter? korenron 3 1,977 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 2,416 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why does unpickling only work ouside of a function? pjfarley3 5 3,444 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 4,239 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  len() function, numbers doesn't work with Geany Editor Penguin827 3 3,001 May-08-2020, 04:08 AM
Last Post: buran
  Powerset function alternative does not work oClaerbout 1 1,997 Feb-11-2020, 11:34 AM
Last Post: Larz60+
  why my function doesn't work cimerio 4 2,884 Jan-20-2020, 08:11 PM
Last Post: cimerio

Forum Jump:

User Panel Messages

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