def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
What these number: 4,5,6 mean and do in the ending of print?
They are arguments/parameters being passed to the function greater_less_equal_5 as the value of answer to be used inside the body of that function. For example, greater_less_equal_5(4) effectively runs this code:
if 4 > 5:
return 1
elif 4 < 5:
return -1
else:
return 0
This has nothing to do with the print.
Related
This question already has an answer here:
why can't return assignment statement [duplicate]
(1 answer)
Closed 2 years ago.
I'm learning Python and I was writting this snippet:
def some_function(a):
if a <= 5:
return b = a + 5
elif a > 5:
return b = a + 20
When i run it, it appears :
return b = a + 5
^
SyntaxError: invalid syntax
However, if i write it in this way, it works:
def some_function(a):
if a <= 5:
b = a + 5
elif a > 5:
b = a + 20
return b
Why does it works this way?
You are not allowed to assign and return a thing in the same line. You can return without assigning a variable like this.
def some_function(a):
if a <= 5:
return a + 5
elif a > 5:
return a + 20
Also, you can do this and save a line since you only have 2 conditions. If a is not less than or equal to 5, it goes to the next line.
def some_function(a):
if a <= 5:
return a + 5
return a + 20
This is also equivalent, else you can't specify a condition, it's a catch all for anything that isn't satisfied earlier.
def some_function(a):
if a <= 5:
return a + 5
else:
return a + 20
Both of these are probably preferable stylistically to using elif. Elif is usually for if you have 3 or more conditions like:
def some_function(a):
if a <= 5:
return a + 5
elif 5 < a < 30:
return a + 7
else:
return a * 100
Learning Python using a textbook and codecademy.
This code is returning a SyntaxError. Can I not use two greater/less than symbols in the same elif statement? Or is the problem my '<='?
def movie_rating(rating):
if rating <= 5:
return "Avoid"
elif rating > 5 and <= 9:
return "I recommend!"
else:
return "Amazing"
The problem is because of your and in line 4, this error happening because when you use and you need to define a new condition in other word you should change your code to one of this codes:
def movie_rating(rating):
if rating <= 5:
return "Avoid"
elif rating > 5 and rating <= 9:
return "I recommend!"
else:
return "Amazing"
or change your code to this:
def movie_rating(rating):
if rating <= 5:
return "Avoid"
elif 5> rating <= 9:
return "I recommend!"
else:
return "Amazing"
if you want to know how you can use and in python, follow this link:
How to use boolean 'and' in Python
and
how use if and else elif or
Im trying to make a while loop in python, but the loop keeps looping infinitely.
Here's what I have so far:
def pvalue(num):
ans = ''
while num > 0:
if 1 <= num <= 9:
ans += 'B'
num -= 1
if num >= 10:
ans += 'A'
num -= 10
return ans
I want num to be returned as ans as follows:
if num is 5, I want ans to be BBBBB
if num is 10, ans is A
if num is 22, I want ans to be AABB.
You may want to learn about the break statement.
As for your code:
def pvalue(num):
ans = ''
while num > 0:
if num >= 10:
ans += 'A'
num -= 10
else:
ans += 'B'
num -= 1
return ans
Is much better, the case when num == 9 is now handled properly
Use the break statement to get out of the loop.
def pvalue(num):
ans = ''
while num > 0:
if 1 <= num <= 9:
ans += 'B'
num -= 1
if num >= 10:
ans += 'A'
num -= 10
if num<=0:
break
return ans
Count up from 1. If the number is a multiple of 7 they say "zap" instead of the number. If the number has a digit of 3 in it they say "buzz" instead of the number, and if both things are true they say "zap buzz".
Devise a function zap_buzz that plays a turn of the game. Given a positive integer parameter it should either return that integer value if neither of the "zap"/"buzz" conditions hold. If some condition holds, it should return the string "zap", the string "buzz", or the string "zap buzz", whichever is appropriate. You are allowed to assume the parameter is less than 1000. As in the earlier exercises, please don't convert the integer to a string to determine its digits.
>>> zap_buzz(8)
8
>>> zap_buzz(14)
'zap'
>>> zap_buzz(13)
'buzz'
>>> zap_buzz(35)
'zap buzz'
^^^^^ is the prompt. I have gotten so far:
def zapbuzz(x):
x =0
if x % 3 == 0 and x % 7 == 0:
return str("ZapBuzz")
elif x % == 3 and x % 7 != 0 :
return str("Zap")
elif x % 3 != 0 and x % 7 == 0:
return str("Buzz")
else /* x % 3 >= 1 or x % 7 >= 1:
return x
****Please don't give me the answer, but a few good tips or maybe a "try thinking about XYZ/etc/whatevertip in a different way would be super awesome. Thank you!
okay I read through the comments, thank you for inspo!
I made the following changes:
n = input()
def ZapBuzz(n):
if n%7 == 0 & n%3 == 0:
return ("ZapBuzz")
elif n%7 == 0 & n%3 != 0:
return ("Zap")
elif n%7 != 0 & n%3 == 0:
return ("Buzz")
else:
return (n)
okay, just talked to a tutor.... I worked out the function, except the issue im having now is that when i input 1 into the function, the terminal spits out 'none'.
def zap_buzz(x):
i = 0
t = False
while i < 3:
if ((x // 10**i)%10) == 3:
t = True
i += 1
if t == True and x % 7 == 0:
return "zap buzz"
if x % 7 == 0:
return "zap"
if t == True:
return "buzz"
I've started the book "Automate The Boring Stuff" by Al Sweigart.
At the end of Chapter 3, the author suggests creating a Collatz Sequence in Python as a practice exercise. (the practice exercise suggests I use a the print function and return statement)
When I use a print() function in my code, it works great and I get all the evaluated values I want to see on the screen:
print("This is The Collatz Sequence")
user = int(input("Enter a number: "))
def collatz(n):
print(n)
while n != 1:
if n % 2 == 0:
n = n // 2
print(n)
else:
n = n * 3 + 1
print(n)
collatz(user)
Question:
How come when I want to use the return statement, the while loop only runs once?
For example, passing the integer 3 into my function with the return statement only gives me the return value of 3 and 10:
print("This is The Collatz Sequence")
user = int(input("Enter a number: "))
def collatz(n):
print(n)
while n != 1:
if n % 2 == 0:
n = n // 2
return n
else:
n = n * 3 + 1
return n
result = collatz(user)
print(result)
return exits the function and, therefore terminates your while loop.
Perhaps you meant to use yield instead:
print("This is The Collatz Sequence")
user = int(input("Enter a number: "))
def collatz(n):
print(n)
while n != 1:
if n % 2 == 0:
n = n // 2
yield(n)
else:
n = n * 3 + 1
yield(n)
print(list(collatz(user)))
Output:
This is The Collatz Sequence
Enter a number: 3
3
[10, 5, 16, 8, 4, 2, 1]
Yield is logically similar to a return but the function is not terminated until a defined return or the end of the function is reached. When the yield statement is executed, the generator function is suspended and the value of the yield expression is returned to the caller. Once the caller finishes (and assumably uses the value that was sent) execution returns to the generator function right after the yield statement.
In your code you don't re-feed the new value back into your equation. Try separating your while loop from the collatz module. I have an example of this below:
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
chosenInt = int(input('Enter an integer greater than 1: '))
print(chosenInt)
while chosenInt != 1:
chosenInt = collatz(chosenInt)
print(chosenInt)
def collatz(number):
if (number%2 == 0):
return print(number//2);
else:
return (print(number*3+1));
inputNumber = input("Enter a number greater than 1:");
result = collatz(int(inputNumber));
while result != 1:
result = collatz(result);
I am getting a typeError with it! Don't know why?