Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 learning to code python nhan Unladen Swallow Posts: 3 Threads: 1 Joined: May 2020 Reputation: 0 Likes received: 0 #1 May-23-2020, 01:25 PM (This post was last modified: May-23-2020, 01:28 PM by nhan. Edited 1 time in total.) Hi guys, I just started learning to code and using Python, I am working on a beginner exercise and the result is not what I am supposed to get, I am hoping you guys could point me in the right direction on how to correct it. I didn't get any error, but the last print line only printed out the the string "Calories content for and the date" but it does not print out the number of totalcal. When I removed the str(today) from that line, it printed out the totalcal. If both str(today) + str(totalcal) are on the same line, then it would only printed out the value for str(today), so it would only be one or the other. What am I doing wrong? Thank you so much for your help, please let me know if you have any questions. ```from datetime import date today = date.today() print("Today's date? " + str(today)) print("Breakfast Calories? ") breakfast_cal = int(input()) print("Lunch Calories? ") lunch_cal = int(input()) print("Dinner Calories? ") dinner_cal = int(input()) print("Snack Calories? ") snack_cal = int(input()) totalcal = breakfast_cal + lunch_cal + dinner_cal + snack_cal print(totalcal) print("Calories content for " + str(today) + str(totalcal) )``` GOTO10 Silly Frenchman Posts: 49 Threads: 1 Joined: Apr 2020 Reputation: 10 Likes received: 4 #2 May-23-2020, 02:19 PM Your code is printing out both the date and the total calories in the final line, but with no space between them (since you haven't indicated one in the code). There are tons of different ways of formatting print output and strings in python, but a simple change to your code to add a colon and space after the date would look like: ```print("Calories content for " + str(today) + ': ' + str(totalcal)) ``` Dotparsed Unladen Swallow Posts: 2 Threads: 1 Joined: May 2020 Reputation: 0 Likes received: 0 #3 May-23-2020, 02:21 PM (This post was last modified: May-23-2020, 02:21 PM by Dotparsed. Edited 1 time in total.) Hi, im too new in Python, but maybe you can try this `print("Calories content for ",str(today),str(totalcal))`or `print("Calories content for ",today," ",totalcal)` nhan Unladen Swallow Posts: 3 Threads: 1 Joined: May 2020 Reputation: 0 Likes received: 0 #4 May-23-2020, 02:30 PM OH!! THANK YOU!!! That's awesome, thank you so much! I can't believe I missed that. jefsummers Verb Conjugator Posts: 551 Threads: 1 Joined: May 2019 Reputation: 50 Likes received: 73 #5 May-23-2020, 03:15 PM A good habit to get into with your output is to use f strings. By using the plus sign if today and totalcal were both numeric you would end up with the sum, which is clearly not intended. `print(f"Calorie content for {today} is approximately {totalcal}. You should eat less")`Very readable, less prone to error. nhan Unladen Swallow Posts: 3 Threads: 1 Joined: May 2020 Reputation: 0 Likes received: 0 #6 May-23-2020, 03:35 PM Thanks Jefsummers, cool tips! I saw someone using the f string at another forum and didn't really understand how it works until now. Also, just testing around to see the difference from using Dotspared's method above, if using the + sign from my original method, I would have to cast today and totalcal to string, otherwise, it would bombed out. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Learning python, stuck on some code. stanceworksv8 2 713 Apr-02-2019, 01:51 AM Last Post: stanceworksv8 Still learning - code efficiency, which of these is better? shelzmike 2 1,182 Oct-14-2017, 04:47 AM Last Post: shelzmike

Forum Jump:

Users browsing this thread: 1 Guest(s)