Function with branch - python

Define function print_popcorn_time() with parameter bag_ounces. If bag_ounces is less than 3, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bag_ounces followed by "seconds". End with a newline. Example output for bag_ounces = 7:
42 seconds.
For this program I'm getting suck on an error:
"File "main.py", line 6
elif bag_ounces = bag_ounces * 6"
^
def print_popcorn_time(bag_ounces):
if bag_ounces < 3:
print("Too small")
elif bag_ounces > 10:
print("Tool large")
elif bag_ounces = bag_ounces * 6
else:
print(bag_ounces)
print_popcorn_time(7)

Your last condition should just be else
def print_popcorn_time(bag_ounces):
if bag_ounces < 3:
print("Too small")
elif bag_ounces > 10:
print("Tool large")
else:
print('{} seconds'.format(bag_ounces * 6))

Here is the proper code. Tested in ZyBooks. The "else" print statement needed to be reformatted. Cheers.
def print_popcorn_time(bag_ounces):
if bag_ounces < 3:
print('Too small')
elif bag_ounces > 10:
print('Too large')
else:
print((bag_ounces * 6), 'seconds')
print_popcorn_time(7)

def print_popcorn_time(bag_ounces):
if bag_ounces < 3:
print('Too small')
elif bag_ounces > 10:
print('Too large')
else:
print(f'{bag_ounces * 6} seconds')
user_ounces = int(input())
print_popcorn_time(user_ounces)

def print_popcorn_time(bag_ounces):
if bag_ounces < 3:
print("Too small")
return
if bag_ounces > 10:
print("Too large")
return
else:
print(str(6 * bag_ounces)+ " seconds")
user_ounces = int(input())
print_popcorn_time(user_ounces)

Related

Python print day name of week from number

I'm creating a program which I have to put a number and when I run the program, the solution should be the day for that number. But I don't know how to do. The program I did was this:
num = 4
if(num = 1)
print('Monday')
elif(num = 2)
print('Tuesday')
elif(num = 3)
print('Wednesday')
elif(num = 4)
print('Thursday')
elif(num = 5)
print('Friday')
elif(num = 6)
print('Sunday')
elif(num = 7)
print('Saturday')
elif(num < 1)
print('Put a number between 1 and 7')
else(num > 7)
print('Put a number between 1 and 7')
In python, for statements like if, for, etc. You have to add : at the end of it.
And for comparing (for equal) you have to use == and not =
num = 4
if(num == 1):
print('Monday')
elif num == 2:
print('Tuesday')
.....
You can compare without parenthesis and it will work too.
num = 0
while num <= 7:
num += 1
if num == 1:
print('Monday')
elif num == 2:
print('Tuesday')
elif num == 3:
print('Wednesday')
elif num == 4:
print('Thursday')
elif num == 5:
print('Friday')
elif num == 6:
print('Saturday')
elif num == 7:
print('Sunday')

Cannot get rid of elif invalid syntax in python

def func(val):
num = int(input("Enter a number:"))
if num>val:
print ("Too high!")
return 1
elif num:
print ("Too low!")
return -1
else:
print ("Got it!!")
return 0
ch=1
while(ch!=0):
ch=func(15)
I keep getting the error:
"elif num:
^
SyntaxError: invalid syntax"
Is it simply a formatting issue that is causing this error message? or my code?
return statements should be indented
def func(val):
num = int(input("Enter a number:"))
if num>val:
print ("Too high!")
return 1
elif num:
print ("Too low!")
return -1
else:
print ("Got it!!")
return 0
ch=1
while(ch!=0):
ch=func(15)

Simple Python Game while-loop syntax error

So - I'm new to Python and just messing around with some basic games to get my head around the language.. For whatever reason this little code returns "Invalid Syntax on Line 8" which is the "While guessedwrong == 1" line... Can anyone tell me why?
from random import randint
UserResponse = int(input("Guess what number between 1 and 100 I'm thinking
of!"))
RandomNumber = randint(1,100)
guessedwrong = 1
While guessedwrong == 1:
If UserResponse > RandomNumber:
print("Nope! Lower!")
UserResponse = int(input("Try again!"))
elif UserResponse < RandomNumber:
print("Njet! Higher!")
UserResponse = int(input("Try again!"))
else
print("Correct! You're awesome!")
GuessedWrong = 0
Python is case sensitive:
while guessedwrong == 1:
#^
if UserResponse > RandomNumber
#^
# Some other your code
else:
# ^^
print("Correct! You're awesome!")
guessedwrong = 0
# ^^^^^^
Full corrected code
from random import randint
UserResponse = int(input("Guess what number between 1 and 100 I'm thinking of!"))
RandomNumber = randint(1, 100)
guessedwrong = 1
while guessedwrong == 1:
if UserResponse > RandomNumber:
print("Nope! Lower!")
UserResponse = int(input("Try again!"))
elif UserResponse < RandomNumber:
print("Njet! Higher!")
UserResponse = int(input("Try again!"))
else:
print("Correct! You're awesome!")
guessedwrong = 0

python: grading assignment, negative value

I am working on a python lettering assignment.
90 or above is an A and so on and so on for the rest of the letter grades; but when a value is inputted as a negative number, I need the code to do nothing other than display an error.
This is what i tried so far:
#Design a Python program to assign grade to 10 students
#For each student, the program first asks for the user to enter a positive number
#A if the score is greater than or equal to 90
#B if the score is greater than or equal to 80 but less than 90
#C if the score is greater than or equal to 70 but less than 80
#D if the score is greater than or equal to 60 but less than 70
#F is the score is less than 60
#Ihen the program dispalys the letter grade for this student.
#Use while loop to repeat the above grade process for 10 students.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif (num < 60 and <= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
Original screenshot
I see three problems, all in the same line:
elif (num < 60 and <= 0:
Syntax: num < 60 and <= 0 is not a valid expression; should be num < 60 and num <= 0
Logic: num <= 0 is not what you want, it should be num >= 0
Syntax: you missed a closing bracket ).
If you change those, it should work.
grade = int(input("Enter Score:"))
print "FFFFFDCBAA"[grade//10] if grade >= 0 else "ERROR!!!!"
you just have to change your elif for below 60.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif 60 > num >= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")

If comparison statements in Python

This block of code returns "cat", "dog", "hamster", and "unicorn", but it shouldn't return "unicorn" at all! Is there any reason for this?
if random.randint(0,10) < 5:
print("dog")
elif random.randint(0,10) > 5:
print("cat")
elif random.randint(0,10) == 5:
print("hamster")
else:
print("unicorn")
You're getting new random number on each comparison. What you probably meant is:
my_random_int = random.randint(0,10)
if my_random_int < 5:
print("dog")
elif my_random_int > 5:
print("cat")
elif my_random_int == 5:
print("hamster")
else:
print("unicorn")
random.randint is called again each time it is reached, potentially producing a different result each time (since that is the function's purpose).
If you want to repeatedly test with the same value, then store the value first.
You should create the random number only once!
val = random.randint(0,10)
if val < 5:
print("dog")
elif val > 5:
print("cat")
elif val == 5:
print("hamster")
else:
print("unicorn")
Assuming correct indentation, there's no reason for three random ints to be respectively >=5, <=5, and "not 5".
You probably meant to do this:
value = random.randint(0, 10)
if value < 5:
print("dog")
elif value > 5:
print("cat")
elif value == 5:
print("hamster")
else:
print("unicorn")
Now there are no chances of unicorns.
Your random number is different everytime you call random.randint so it might be 7 when you test the first if and go past it, then 3, then 4, and bam, you're in unicorn.
You should call random.randint only once at the beginning of your if, save its value and check it instead.
myrand = random.randint(0,10)
if myrand < 5:
print("dog")
elif myrand > 5:
print("cat")
elif myrand == 5:
print("hamster")
else:
print("unicorn")
The issue here is that you're generating a new random number each time. You should create it once and then assign it to a variable, then check that.
You're generating three different random numbers. What you're thinking is this:
random_number = random.randint(0,10)
if random_number < 5:
print("dog")
elif random_number > 5:
print("cat")
elif random_number == 5:
print("hamster")
else:
print("unicorn")
This code will only return one word, and will never return "unicorn".
You need to create only one random integer.
Your code should be:
myRandom = random.randint(0,10)
if myRandom < 5:
print("dog")
elif myRandom > 5:
print("cat")
elif myRandom == 5:
print("hamster")
else:
print("unicorn")

Categories