Python. Noob. Syntax error [duplicate] - python

This question already has answers here:
Else Syntax Error Python
(7 answers)
Closed 6 years ago.
A = int(input()) #recom. sleep time
B = int(input()) #unrecom. sleep time
H = int(input()) #fact. slept
if (A <= H < B ):
print('Fine')
elif (A < H >= B):
print('too much')
else (A > H < B):
print('not much')
Error:
File "<ipython-input-9-12133f4a4c3d>", line 8
else(A > H < B):
^
SyntaxError: invalid syntax
Guys don't be rude, I'm financial and start learning programming :)
I can't see the difference between code who passed errors check and this one.

You are using else to make a test. You should use elif
A = int(input()) #recom. sleep time
B = int(input()) #unrecom. sleep time
H = int(input()) #fact. slept
if A <= H < B :
print('Fine')
elif (A < H >= B):
print('too much')
elif A > H < B:
print('not much')

Related

There's an error in the process of receiving numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
n = int(input())
num = n
cnt = 0
while True:
a = num // 10
b = num % 10
c = (a + b) % 10
num = (b * 10) + c
cnt += 1
if num == n:
print(cnt)
break
C:/Users/ADMIN/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ADMIN/loc-git/Study_CodingTest/baekjoon/Bronze/1110번.py
Traceback (most recent call last):
File "c:\Users\ADMIN\loc-git\Study_CodingTest\baekjoon\Bronze\1110번.py", line 2, in <module>
n = int(input())
ValueError: invalid literal for int() with base 10: '& C:/Users/ADMIN/AppData/Local/Programs/Python/Python310/python.exe c:/Users/ADMIN/loc-git/Study_CodingTest/baekjoon/Bronze/1110번.py'
python version = 3.10.0
I Dont't understand what you need. but chack this code don't come any error:-
n = int(input("enter number"))
num = n
cnt = 0
while True:
a = num // 10
b = num % 10
c = (a + b) % 10
num = (b * 10) + c
cnt += 1
if num == n:
print(cnt)
break

How to raise a Valueerror for a number that exceed a specific value in python?

I have tried to create a function that takes the factorial of a non-negative integer n. And that part is working great, but I also have to create a ValueError if input is below 0 or above 12 which doenst work.
def factorial(n):
countdown = n
factorial_sum = 1
while True:
try:
if n == 0:
return 1
if n < 0 or n > 12:
raise ValueError
except ValueError:
return 'Error'
if (countdown / n) > 0 and n <= 12:
factorial_sum = factorial_sum * countdown
countdown = countdown - 1
if (n - countdown + 1) == n:
return factorial_sum
elif (n - 1) == 0:
return factorial_sum
The challenge is from codewar and state:
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1.
Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw an exception of type ArgumentOutOfRangeException (C#) or IllegalArgumentException (Java) or RangeException (PHP) or throw a RangeError (JavaScript) or ValueError (Python) or return -1 (C).
All answers will be greatly appreciated
Replace
try:
if n == 0:
return 1
if n < 0 or n > 12:
raise ValueError
except ValueError:
return 'Error'
by
if n == 0:
return 1
if n < 0 or n > 12:
raise ValueError

NameError: name 'number' is not defined [duplicate]

This question already has answers here:
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Why does this not work as an array membership test? [duplicate]
(1 answer)
Closed 4 years ago.
I have a code like below
a=25
b=20
#number=0
if a < b == True:
number = 1
elif a >b == True:
number = 2
print(number)
When i execute this i get the following error
NameError: name 'number' is not defined
When i initialize the number = 0 like below
a=25
b=20
number=0
if a < b == True:
number = 1
elif a >b == True:
number = 2
print(number)
then i am not getting the output as 2, i am getting 0 instead, what am i missing here
Put a parenthesis around the condition.
Ex:
a=25
b=20
#number=0
if (a < b) == True:
number = 1
elif (a > b) == True:
number = 2
print(number)
or:
if a < b:
number = 1
elif a > b:
number = 2
print(number)
You are currently doing (a < b) and (b == True) & (a > b) and (b == 20)

Comparing three numbers? [duplicate]

This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Closed 8 years ago.
I'm in my intro to programming class and for some reason am a bit stumped as to how to proceed from here. Basically the prompt is to compare three numbers the user inputs and see if the first number is between the last two.
def fun1(a,b,read,):
if a < read and read > b:
return print("Yes")
elif b < read and read > a:
return print("Yes")
else:
return print("No")
def main():
read = input("mid: ")
a = input("num1 ")
b = input("num2 ")
fun1(read,a,b,)
print("result:",fun1)
So as you see I can't figure out how to get the comparing function down in the first function. Any help is much appreciated!
Python allows you to chain comparison operators:
if a < b < c:
This will test if b is between a and c exclusive. If you want inclusive, try:
if a <= b <= c:
So, in your code, it would be something like:
if a < read < b:
return print("Yes")
elif b < read < a:
return print("Yes")
else:
return print("No")
or, more concisely:
if (a < read < b) or (b < read < a):
return print("Yes")
else:
return print("No")
Note too that print always returns None in Python. So, return print("Yes") is equivalent to return None. Perhaps you should remove the return statements:
if (a < read < b) or (b < read < a):
print("Yes")
else:
print("No")

Why isn't this 'if' statement working?

I have created an if statement in Python, it is a password strength checker. I think it might be the numbers, but I have no clue.
When I type in; aabbcc3
it is meant to be MEDIUM, but it comes up with WEAK. Same as RTH14hl as it comes up with MEDIUM but it's meant to be STRONG.
if u >=6 or l >=6 or n >=6:
print("weak")
elif u > 1 and n > 1:
print("medium")
elif l > 3 and n >= 1:
print("medium")
elif u > 1 and l> 1:
print("medium")
elif u > 3 and l > 2 and nums > 2:
print("strong")
The problem is that the order in which the statements are set up is producing an effect that you do not want. For example:
elif u > 1 and l> 1:
print("medium")
elif u > 3 and l > 2 and nums > 2:
print("strong")
The last line here will never be executed. Because anything that makes the last conditional true will make the previous conditional be true.
For example if u is 4 and l is 4 then:
elif u > 1 and l> 1:
becomes:
elif 4 > 1 and 4 > 1:
which will evaluate to True and print "medium"
You can solve this issue by rearranging the order of the statements like so:
elif u > 3 and l > 2 and nums > 2:
print("strong")
elif u > 1 and l> 1:
print("medium")
Essentially you want the hardest things to match be at the top then let non matches fall down to easier to match cases, not the other way around.
Also from the comment made I think it's highly likely that you probably want to generate the u l and n values differently to how you currently are doing it. Python has a nice feature called generator expressions that along with some library function will make your code much more maintainable.
Instead of:
u = p.count("A") + p.count("B") + p.count("C"), ...
l = p.count("a") + p.count("b") + p.count("c"), ...
n = p.count("1") + p.count("2") + p.count("3"), ...
you can do:
u = sum((char.isalpha() for char in p))
l = sum((char.islower() for char in p))
n = sum((char.isdigit() for char in p))

Categories

Resources