I am in an intro to programming class and we're making a calculator to figure out how many times a human heart has beaten given the number of years someone has been alive, and I keep getting unexpected EOF while parsing on the last line of code, can someone help me figure out why?
input("What is your name? ")
age= float(input("How old are you? "))
print("The average human heart beats 70 times a minute.")
beats_per_hour = 70 * 60
beats_per_day = beats_per_hour * 24
beats_per_year = beats_per_day * 365
beats_age = beats_per_year * age
print(format("Beats per minute: 70"))
print(format("Beats per hour:",beats_per_hour))
print(format("Beats per day:",beats_per_day))
print(format("Beats per year:",beats_per_year))
print(format("Your heart has taken",beats_age,"beats, since you were born.")enter code here
I believe that your immediate problem is that you forgot the closing parenthesis on the last line: note how you have only one R_PAREN at the end of that one, but two on all the others. Also, do you want to save the user's name in line 1?
Here's a link to help with keyboard input.
Related
I want to create a game where a person is drawn out randomly.
Can some1 check the code if everything is setup correctly.
I have tested the code numerous times and its ok in my eyes.
But when I send the code to a review, to an online class I only get 50% score.
import random
# 🚨 Don't change the code below 👇
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
print(names)
names_count = len(names)
random_name_number = random.randint(0, names_count)
print(f"{names[random_name_number]} is going to buy the meal today!")
The problem here is almost certainly with randint. Unlike most Python conventions, the parameters to randint are inclusive. That means, if you supply (0,10), you are going to get numbers from 0 to 10. In your case, if they supply 10 names, using index 10 is going to cause an exception.
You want random.randrange, not random.randint. randrange(0,10) will supply numbers from 0 to 9.
total_cost_of_food = float(input('How much did the food cost? '))
sales_tax = 8.25
tax_plus_total = total_cost_of_food * sales_tax / 100
print("Your tax for this meal was:", tax_plus_total)
x = bool(input("Would you like to add a tip? ")
if x is False
print("Thanks, your total for today is:", tax_plus_total)
else
print("That is", x, "I would like to add a tip")
I keep getting this error for my print statement that is under the if statement, it says it's a syntax error but I don't see anything wrong with the syntax... I have only been coding for about a week though so I am likely missing something.
The : is missing, at the end of the if line and the else line
Instead of if x is False, you should use if not x:
Also, you should reduce the indenting of all four lines in your if-else construct, by one level.
bool(input("Would you like to add a tip? ") lacks the second closing parenthesis.
Python very often prints a syntax error for the next line or even a subsequent line after that when you have a missing closing parenthesis. It tries to make sense of what you wrote and it takes a while for it to figure out that it really can't.
This also masks the indentation error on the following line, and the lack of a colon at the end of the if and else lines.
I made a Python program to repeat the number of internships that I applied for, BUT I need it to store the amount I add so that I can ask it later how many I applied for, rather than me having to enter a number every time. Also, I want it to be able to update if the number of internships I applied for changes, I will input the new number of internships manually. How can I change my program to do that? Please take a look thanks
print("Welcome back, Ryan")
internships = {}
asking_ryan = True
amount = input("Enter how many internships you have applied for: ")
amount = int(amount)
if amount > 1:
print("You have applied for: " + str(amount) + " internship(s)")
str(amount)
if amount < 1:
print("Error! you cannot apply for 0 internships!")
if amount == 1:
print("You have applied for: " + str(amount) + " internship")
Program output:
Welcome back, Ryan.
Enter how many internships you have applied for: 2
You have applied for: 2 internship(s)
I saw your question. I kind of understand it.
You want to be able to update the internships you have done so you don't have to always rerun the program so it counts your internship?
You might have to use some files for that, should be simple!
First, you can make a file called opens.txt and add a 0 to the file that tracks how many times you opened that program. When you run the program do this:
opens = open("opens.txt", w+)
open_count = int(opens.read())
opens.write(open_count + 1)
if open_count == 1:
amount = input("Enter how many internships you have applied for: ")
... # Other Code
Then make a file called something like internships.txt where it will store how many internships you currently have, default it to 0.
internships = open("internships.txt", w+)
internship_count = int(internships.read())
print("You currently have applied to {} internships since last time...".format(internship_count)
new_internships = input("How many new internships have you applied for? ")
internships.write(internship_count + new_internships)
I think this should help? Haven't used files in a long time. Next time please phrase your question a bit better.
I am trying to write a program to do some simple power usage and cost calculations, the logic is correct and the algorithm works for VB but I want to use Python. Being new to Python I am a little confused as to what I am missing, the code is below. I have used some conventions here to help me understand what is happening
IntWatts = input ("What is the Watt rating? ")
IntHoursOfUse = input ("How many hours is the device on for? ")
IntTariff = input ("What is the Tariff in cents? ")
IntEnergyRating = int(IntWatts)/1000
IntUsageKWH = int(IntEnergyRating) * IntHoursOfUse
IntCostInCents = IntUsageKWH * int(IntTariff)
IntCostInDollars = float(IntCostInCents) / 100
print('the Cent cost is :',IntCostInCents)
print("the Dollar cost is ;", IntCostInDollars)enter code here
I have been using the inputs of
Watts 240
HoursOfUse 5
Tariff 34.1
Thanks
As to why the error is happening, it is simply a matter of typecasting the wrong variable, note the example below.
watts = input('What is the Watt rating?')
hours_of_use = input('How many hours is the device on for?')
tariff = input('What is the Tariff in cents?')
energy_rating = int(watts) / 1000
usage_kwh = energy_rating * int(hours_of_use)
cost_in_cents = usage_kwh * int(tariff)
cost_in_dollars = cost_in_cents / 100
print('The cent cost is :', cost_in_cents)
print('The dollar cost is :', cost_in_dollars)
This code should produce the results you are looking for. As to what some of the problems are here.
A few things to note, you only need to cast the input() values here since they are coming in as strings and need to be interpreted in your program as integers.
In Python there are two forms of division / which results in expected results according to how we as humans learn math, and // which will floor your result; this is why you do not need to cast to floats.
There are many pythonic things here to take away as you learn this language, while I won't go into great depth, do note the naming conventions. Python variables are typically _ delimited, lowercase and self-documenting. Additionally it is considered fairly poor practice to label variables with type, this is an old convention that has fallen out of practice.
For additional reading on Python, check out the coding guidelines:
https://web.archive.org/web/20111010053227/http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting
you need to type-cast IntHoursOfUse to int while initializing IntUsageKWH as:
# v Type casting this to int()
IntUsageKWH = IntEnergyRating * int(IntHoursOfUse)
# ^ No need to type-cast this as you are already doing it in previous line
I needed to change the inputs to be
IntWatts = int(input ("What is the Watt rating? "))
IntHoursOfUse = int(input ("How many hours is the device on for? "))
IntTariff = float(input ("What is the Tariff in cents? "))
and then remove all other int/float commands
Thanks for the help
I am quite behind on my coursework and I am stuck on developing my code for task 2.
My program must allow a user to enter a series of bar codes, search a text file for the codes and then, if found, ask the user for the quantity of the product they wish to purchase. Finally it should print a receipt of the total products bought and the total price.
However, my code does not work when I have to search for the code in my file, it just keeps looping and asking the user to enter their bar code again.
Here is my code so far:
loop=True
while loop==True:
print ("------STOCK LIST------")
print ("a - Place an order by barcode")
print ("b - Place an order by product name")
print ("x - Exit")
task=input("Please make your selection\n")
if task.lower()=="a":
print("------STOCK FILE LOADING------")
myfile=open("barcode.txt", "r") #this opens the text file
details=myfile.readlines() #reads the file and stores it as the variable 'details' #myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if len(digits) > 8 or len (digits) < 8: #if the digits are longer or shorter than 8 digits, the code is not accepted
print("Please enter a GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in details:
if digits in line:
productline=line
myfile=open("receipt.txt", "r") #opens receipt file
myfile.writelines("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
itemsplit=itemline.split(' ') #seperates into different words
price=float(itemsplit[2]) #price is
total=(price)*(quantity) #this works out the price
myfile.writelines("Your total spent on this product is: " +str("£:,.2f)".format(total)+"\n"))
else:
break
If you could help me I would be very grateful as none of my classmates will help me and if so, can you keep the code quite simple as I am not the best at coding?
I'm doing GCSEs myself right now so I appreciate the difficulty with coursework. Careful asking people for code though - I'm pretty sure that can get you disqualified in certain specifications (although I'm not doing the same as you).
I see two main problems in your code:
1. The indentation is incorrect (if I've correctly understood what you're trying to do)
I think it should be like this:
while loop==True:
print ("------STOCK LIST------")
print ("a - Place an order by barcode")
print ("b - Place an order by product name")
print ("x - Exit")
task=input("Please make your selection\n")
if task.lower()=="a":
print("------STOCK FILE LOADING------")
myfile=open("barcode.txt", "r") #this opens the text file
details=myfile.readlines() #reads the file and stores it as the variable 'details' #myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if len(digits) > 8 or len (digits) < 8: #if the digits are longer or shorter than 8 digits, the code is not accepted
print("Please enter a GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in details:
if digits in line:
productline=line
myfile=open("receipt.txt", "r") #opens receipt file
myfile.writelines("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
itemsplit=itemline.split(' ') #seperates into different words
price=float(itemsplit[2]) #price is
total=(price)*(quantity) #this works out the price
myfile.writelines("Your total spent on this product is: " +str("£:,.2f)".format(total)+"\n"))
else:
break
Right now, if the code inputted by the user is correct, you're breaking out of the entire while loop as soon as the code is validated. The program does not have time to do the next steps in the for loop.
2. The second if statement
if len(digits) > 8 or len (digits) < 8: #if the digits are longer or shorter than 8 digits, the code is not accepted
print("Please enter a GTIN-8 code\n")
else:
This can be made better. You can write a new if statement instead so that it breaks the while loop only if the code entered is the correct length.
Your current statement asks me to re-input the code twice every time I get it wrong, rather than only asking me once each time I input something incorrect.
I'm about to go work on your other question.
Good luck!