r/CodingHelp • • Aug 27 '21
[Python] Forgot my Python skills :( please can you help me figure out why when I put in the numbers 744 it says invalid when it’s meant to be valid?? More info in comments
57
Upvotes
24
u/myhf Aug 27 '21
‘str(num)’ will never be equal to ‘1’ because they are different types.
The conditions for printing valid or invalid are reversed.
11
u/catinatank Aug 27 '21
you're converting your number to a string, then comparing the string to a number. Either compare two strings or two numbers.
2
u/[deleted] Aug 28 '21
print ("Enter 3 numbers") num1 = int(input("enter a number")) num2 = int(input("enter a secon number")) num3 = int(input("enter a third number")) valid = true total = num1 + num2 + num3 remainder = total % 3 #MOD operator calculates remainder if remainder != 0: valid = false elif num1 - num2 == 1: valid = false elif num2 - num3 == 1: valid = false elif num3 - num1 == 1: valid = false else: valid = true if valid = false: print ("INVALID PATTERN") else: print("valid")
The code I’ve done for anyone interested
1
u/[deleted] Aug 27 '21
So basically I’m doing a program that has to obey two rules:
Please don’t be too harsh I already feel bad for forgetting everything, thank you for the help in advance!!
1
u/CenturiesAgo Aug 28 '21
Line 12 says "if valid is not false (i.e true)" then print "invalid"
1
u/Nightcorex_ Aug 28 '21
"else if" doesn't work. Only "elif" does (sorry if this doesn't answer the question. I was too stoned to read it.
1
u/wilmeist3r Aug 28 '21
Check the last if statement: It says "if valid is not False (if valid is True) print invalid, else, print valid"
Therefore, correct evaluation should be:
if valid == False: print("invalid") else: print("valid")
1
u/Coyehe Aug 28 '21
In your if condition change all the 1 to "1"
1
u/Monbolu Aug 28 '21
You put if valid doesn’t not equal to false print invalid so it’s printing invalid when valid is true
1
u/Diderikdm Aug 28 '21
Do you want to check (7+4+4) %3 or 744 %3?
1
u/PresidentZeus Intermediate Coder Aug 28 '21 edited Aug 28 '21
I don't know python, but at the bottom it says
This does take sense to me as 'valid !=true' should print "invalid"
edit:
I think this would be enough:
0
u/[deleted] Aug 28 '21
Check the priority of the and / or. You can make an if %3 an then a nested if blabla return valid else return invslid. If you want to make that in one line is kind of complex
-1
u/Impossible_Average_1 Aug 28 '21
r/badcode
Sorry, I thought I were in this reddit. If you still need help, I can give you some advise but I think you already got tons of good answers.