Hi, a new learner here. I am trying that free 2 hour course on the OU website for basics in Python. I have passed first few sections and nearing the end, when I encountered a problem with a code that I used before and it worked in earlier sections. Now it doesn’t.
I am supposed to add a tip according to the number of people. If the number is below 5, there will be no tip. But even if I have put in a number 4, it still adds the maximum tip of 15%. Previously that exact code alone worked perfectly.
Any clue what is wrong in here? Hope the image shows properly.
This was the result when interacting with the code:
Price of ordered item 5
Price of ordered item 5
Price of ordered item 5
Price of ordered item 5
Price of ordered item stop
Number of customers in the group 4
Total bill 23.0
VAT included 4.6
I don’t know which version they use, it is on the OU website, interactive content. But yes, that course has been there for a while. It just puzzles me that the code worked on its own when exercising the particular task, but when trying to use the same for a more complex code, it suddenly doesn’t work.
the following line receives a string str while the conditional statements are expecting an integer type. This is part of the conflict.
number = input("Number of customers in the group ")
If you type:
type(number)
you will get:
<class 'str'>
To rectify this issue, add int to the statement as:
number = int(input("Number of customers in the group "))
If you now type:
type(number)
You should now get:
<class 'int'>
Note that the same principle applies to the answer value line above. However, I take it you might want to use a float for that one assuming you have trailing decimals.
To be honest, it doesn’t make sense to ask for the price of a food item and expecting someone to answer stop. You should pose this as a question all its own.
Hm… I had lines between individual sections, I removed them to be able to capture as much of the code as possible, since I cannot expand the window otherwise. I also took the pic to show that tabulation was hopefully correct as it doesn’t look as graphic when in text. But I saved the text as well, pasting below:
expenses = 0
while True:
answer = input(“Price of ordered item”)
if answer == “stop”:
break
else:
price = float(answer)
expenses = expenses + price
number = input (“Number of customers in the group”)
if number > 14:
percentage = 0.15
elif number > 5:
percentage = 0.10
else:
percentage = 0
tip = expenses * percentage
bill = expenses + tip
VAT = bill * 0.20
print “Total bill”, bill
print “VAT included”, VAT
Now added one line between expenses and ‘while True’, but still got final bill 23.
Price of ordered item 5
Price of ordered item 5
Price of ordered item 5
Price of ordered item 5
Price of ordered item stop
Number of customers in the group 4
Total bill 20.0
VAT included 4.0
The integer alteration worked.
The wording ‘stop’ was just for the sake of exercise. It also took me a while to process the instructions and what it wanted me to do earlier in the course with this stop.
This was my very first encounter with python.
Thanks, for simplicity I put each item just 5, for a quick mental judgement. There originally were various numbers with decimals, like normal prices of items. I am too new to all this.
What is “the OU website”? Please keep in mind that you are writing for an international audience here.
In any event, there’s a conflict here. In up-to-date versions of Python, “The integer alteration” (as you put it) is necessary:
But in those same versions of Python, print also requires parentheses:
Please keep in mind that all 2.x versions of Python have been unsupported since Jan 1, 2020:
Using “interactive” implementations of Python on a website can be convenient, but they aren’t reliable. In general, anyone can make a Python tutorial and they don’t necessarily know what they’re talking about. If they try to give you an interactive Python, they don’t necessarily tell you what version of Python it is. Or necessarily know what version. Or know that it matters. (It mostly wouldn’t: the changes are quite slow.)
We’d be happy to help you set up Python on your own computer. The skills involved, IMO, are among the first big steps in treating your computer as your own property, and as a tool rather than an appliance.
For future questions on the forum, please read the pinned thread for details on post formatting. We do want code to be shown as text, but we want it properly formatted so we can read it properly. Indentation is crucial to the logic of Python code, and it’s also important that we get code with ordinary "quotes" (not “smart quotes” which are a different character, that look “like this” - see?). It’s also useful for error messages, because sometimes they’ll have things angle brackets (like <stdin>) that get stripped away (the forum thinks they’re invalid or possibly malicious HTML).