Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to python and cant find where my error is. Python is saying that there is a syntax error but it wont tell me where it is. My task is to make a programme that can count a taxi fare. Here is my code.
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print("The total cost of the journey is £" cost)
else:
print("The total cost of the journey is £" cost)
The error is probably something simple but I can't find it. Thanks in advance.
if you want to print several things with print() function, you need to separate them with commas
try this:
print("The total cost of the journey is £", cost)
You're not concatenating your strings, if you're on python 3.6 or higher use f-strings:
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print(f"The total cost of the journey is £ {cost}")
else:
print(f"The total cost of the journey is £ {cost}")
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
This is the code. I know that the "question" variables are useless, but that's how i like to do it.
I was supposed to make a pay computation program that asks for input about hours worked and rate of pay and pay 1.5 times the rate for hours worked above 40.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', pay + 'dollars.')
else hrs < 40 :
pay = hrs*rate
print ('Your payment is ', pay + 'dollars.')
I tried to nest it a different way and googled python debugger, but nothing helped. Please tell me how to fix this error and where my mistake is so that i can learn. Thank you !
Hope following code will help you.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
hrs = int(hrs)
rate = int(rate)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', str(pay) + 'dollars.')
elif hrs < 40:
pay = hrs*rate
print ('Your payment is ', str(pay) + 'dollars.')
keep in mind that, input() will always return string datatype, and you have to convert into the int for arithmatic operation,
Wherenver you try to concat string, then you have to convert int to string
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
This is my code:
# Get the user's starting weight.
weight = input('What is your starting weight? ')
# Display the weight loss table.
for month in range(1, 7):
weight = -4
print('At the end of month', month,
'your weight will be', weight, 'lbs.')
I guess try changing:
weight = -4
To:
weight -= 4
Also make the input a string, like this:
# Get the user's starting weight.
weight = int(input('What is your starting weight? '))
# Display the weight loss table.
for month in range(1, 7):
weight -= 4
print('At the end of month', month,
'your weight will be', weight, 'lbs.')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I made a birthday program in Python, the program ask from the user the month he were born in, and then calculate the months until the user have birthday.
For example, the user enter the number 5 and the current month is 2, the program output "You have 3 months until your birthday!".
I don't know how to calculate the months until he have birthday correct.
Example:
from datetime import datetime
def Birthday():
CurrentMonth = datetime.now().month
BornIn = input("What month were you born in ? - ")
result = int(BornIn) + CurrentMonth
if int(BornIn) == CurrentMonth:
print("You have already Birthday in this month!")
elif int(BornIn) > 12:
print("Invalid Input!")
else:
print("You have", result , "monthes until your Birthday!")
Birthday()
What mathematical action do I need to do, to calculate the months until his birthday?
Look at line 7, I used + to calculate but obviously it won't work.
Edit:
I need to do result = CurrentMonth - int(BornIn). This should fix the problem.
Your operation is not correct :
You should do:
result = (int(BornIn) - CurrentMonth)%12
Modulo is here to manage case when your birthday is in the next year. Without modulo you will have negative results (here you won't have any problem because we are in December so your birthday can't be in the 13th months)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Hi guys I have been trying to create a working python vending machine for over a month now and it doesn't look like I am making any progress. If somebody could help me that could be great :) Here is my program so far:
print ("Welcome to the Vending Machine\n")
total = 0
dr = 1
sn = 3
money=int(input("How much money do you want to insert"))
print ("Prices: Drinks: £1, Snacks: £3\n")
Drinks = {'Coke','Pepsi','Orange Juice', 'Apple Juice','Water'}
Snacks = {'Chocolate', 'Snickers','Twix','Peanuts','Crisp'}
state = 'subtotal'
while total <= money:
if state != 'total':
print('')
print('')
print ("\nDrinks Menue:")
print(Drinks)
print ("Snacks Menue:")
print(Snacks)
choice = input("\nEnter the item of your choice: ")
if choice in Drinks:
total += dr
elif choice in Snacks:
total += sn
else:
state = 'total'
print("\n\nThat will be a",state,"of £",total)
else:
print("You have exceeded your inserted money good bye")
I'm trying to make the code reject invalid input and stop when the user has gone over his spending limit.
There is only one problem with this when I run this. So, here is the fix with some explanation so you can learn something. It may seem like a simple mistake but we all make them at some point and eventually, indenting will be second nature.
Indents
Unlike some langues, in Python the indentation matters. A lot. Let's look at your first if statement.
if state != 'total':
print('')
print('')
print ("\nDrinks Menue:")
print(Drinks)
print ("Snacks Menue:")
print(Snacks)
choice = input("\nEnter the item of your choice: ")
Can you see the problem? After an if statment, or more precisely, after the :, we need to indent everything else that we want to be carried out by the if statement otherwise Python won't know where to stop! So by making this simple change:
if state != 'total':
print('')
print('')
print ("\nDrinks Menue:")
print(Drinks)
print ("Snacks Menue:")
print(Snacks)
We have no more error and your program now runs. Notice the indents? Just check that they are always right.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to make a program to calculate final grades. The user inputs x amount of assignments, and based on that I am looking to make x variables asking for the grade recieved and the weight of each assignment.
For example,
(user selects 20 assignments)
(as input):
enter assignment1 grade received,
enter assignment2 grade received,
...............
enter assignment20 grade received. (and the same for weights)
There needs to be a variable per assignment and I am unsure how to essentially convert an integer into a variable. (lists are not allowed).
Please feel free to offer suggestions. Thanks
numAssigns = input("How many assignments?: ")
marks = {}
for i in range(numAssignments):
mark = input("Enter the grade obtained on assignment %s: " %i)
weight = input("Enter the weight of assignment %s: " %i)/100
if weight not in marks:
marks[weight] = {}
if mark not in marks[weight]:
marks[weight][mark] = 0
marks[weight][mark] += 1
total = 0
for weight in marks:
for mark in marks[weight]
total += mark*weight*marks[weight][mark]
print("From all your assignments, you have %s% of the total grade of the course" %total)