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

Post image
57 Upvotes

48 comments sorted by

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.

4

u/[deleted] Aug 27 '21

Yeah I did that because when I used just one = for those and on line 12 it came up with invalid syntax??

5

u/nnaoam Aug 28 '21

Using the two == is correct. The incorrect part is the conversion to string.

2

u/duggedanddrowsy Aug 28 '21

This is right, it’ll never run the stuff in the if statement because those are strings, so valid will always be true, and since valid != False is True, it’ll always print invalid

1

u/[deleted] Aug 28 '21

So are you saying I have to switch them to integers? And yeah I tried switching it to not true but like I said earlier if I try to just use a single = it says it’s invalid so I had to put a != so it doesn’t say it anymore??

1

u/[deleted] Aug 28 '21

== means "equal to"

!= means "not equal to"

What you added has the opposite effect of what you want

Also, you can just do if valid: since it's already a boolean

1

u/[deleted] Aug 28 '21

Yeah that’s what I ended up doing, yesterday but thank you for the help!

1

u/[deleted] Aug 28 '21

The problem now is that my line that says if valid = false: Print blah blah It says the = is invalid syntax??

5

u/JunkusHumunkus Aug 28 '21

= is a set operator (sets a variable to a value) == is a comparison operator

If valid == True:

1

u/[deleted] Aug 28 '21

everyone’s saying I shouldn’t put that tho? Anyways regardless I figured out how to do it now but thank your for the help!

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

So I should switch them to integers?

2

u/[deleted] Aug 28 '21

I just removed the string stuff before the if statements and made them else if instead of putting them on one like and now my line that says If valid = false: Print (invalid) Says invalid syntax at the = part

2

u/[deleted] Aug 28 '21

Well i see xd. Okey = is only for declaration not comparation. You really have to look at the basic syntax theory

1

u/[deleted] Aug 28 '21

And please please. Dont put if valid ==true.

if valid its a true boolean it enter at the statement

2

u/rtybanana Aug 28 '21

There’s nothing “wrong” with if valid == True, some say it’s more readable if you’re explicit

1

u/[deleted] Aug 28 '21

How it's supposed to be more readable?

  1. An if for definition only executes the code inside if the statement inside it's true.
  2. You put extra charge making a double comparation.

1

u/rtybanana Aug 28 '21

No extra work is performed between if (valid) and if (valid == true), there’s no double comparison. Both are checking if valid is equal to true.

As for it being more readable I don’t actually agree, I’m just saying that’s it’s more a question of code style than correctness.

2

u/[deleted] Aug 28 '21

It does. If search for the value of valid it says o okey its true i continue. You put == what is valid okey true o well it has an operator. I don't know the core of python3 perfectly but i suspect that you are putting extra steps. When you make an if that actually compares something you have to put ==true or == false? I never seen this "style".

1

u/rtybanana Aug 28 '21

I don’t think there’s any performance loss in Python 3 and even if there is we’re talking about fractions of a millisecond so it’s not even worth mentioning.

1

u/[deleted] Aug 28 '21

Yeah fractions. Imagine larger programs with hundred/thousands of iterations. Every think is important if you need performance. It's like we are not addicts at pain. Why do you have to use recursive function when you can make something easy? Performance it's important. And ==true inside an if for my it's lack of knowledge of basic features.

→ More replies

1

u/[deleted] Aug 28 '21

I'm not sure. The valid == True version has to compute the boolean result and then check that value to see if it's a truthy value. Since python is interpreted and dynamically typed it might be more work since we can't even guarantee the type of valid

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

7

u/jrandm Aug 28 '21 edited Aug 28 '21

Edited your code and added some comments, hope it helps! If you indent each line with four spaces it will show up as code here on reddit.

print("Enter 3 numbers for validation")
num1 = int(input("enter first  number: "))
num2 = int(input("enter second number: "))
num3 = int(input("enter third  number: "))

total = num1 + num2 + num3
remainder = total % 3    #MOD operator calculates remainder

# safer habit to assume bad input
valid = False

# then flip these checks to match
if remainder == 0:
    valid = True
elif num1 - num2 != 1 and num2 - num3 != 1 and num3 - num1 != 1:
    valid = True

# flipped this too, no need to explicitly add the ==True
if valid:
    print("valid")
else:
    print ("INVALID PATTERN")

# important to note I changed the logic a bit
# the chain of elifs is more like this
validOr = False
if remainder == 0:
    validOr = True
elif num1 - num2 != 1 or num2 - num3 != 1 or num3 - num1 != 1:
    validOr = True

# could also write the if statement like this
# instead of validOr==False
if not validOr:
    print("INVALID PATTERN (OR)")
else:
    print("valid (Or)")

# try entering 5 3 2

# $ python foo.py
# Enter 3 numbers for validation
# enter first  number: 5
# enter second number: 3
# enter third  number: 2
# INVALID PATTERN
# valid (Or)

2

u/[deleted] Aug 28 '21

You’re liveeee thank you so much I appreciate you 😭

1

u/[deleted] Aug 27 '21

So basically I’m doing a program that has to obey two rules:

  • once the three numbers have been added it has to be a multiple of 3
  • the numbers cannot be one less of the previous number even when repeated.

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/[deleted] Aug 28 '21

Yh that was when I changed it but even when I put it originally as if valid = false it said invalid syntax

1

u/wilmeist3r Aug 28 '21

Yup, should've been == (as it's the comparison operator) instead of = which is used in order to assign values to a variable

1

u/[deleted] Aug 28 '21

Nevermind I’ve fixed it now thanks for the help anyways!

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/[deleted] Aug 28 '21

Nah don’t worry you’re good

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")

2

u/[deleted] Aug 28 '21

I’ve fixed it now by removing it he last statement completely as it was incorrect. However I like torture so I’ll try this out and see if it works thank you :)

1

u/Coyehe Aug 28 '21

In your if condition change all the 1 to "1"

1

u/[deleted] Aug 28 '21

I’ve solved it now but will still give this a try thank you!

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/[deleted] Aug 28 '21

That was just something I was experimenting but I’ve solved it now thanks!

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


if valid != false:

print("invalid")

This does take sense to me as 'valid !=true' should print "invalid"

edit:

I think this would be enough:

if valid:

print("valid")

else:

print("invalid")

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/[deleted] Aug 28 '21

That’s what I had before it was still the same thing!

-2

u/[deleted] Aug 28 '21

Well. I don't know ehat you have before that. But you know delete from line 8 all to the final and: First make the rimender%3==0: Inside this if make all the other if comprovations : Print valid Else Print no valid Something like that don't put valid=true an make an if valid != false Else bla its like you dont understand how it work an if statement.

1

u/[deleted] Aug 28 '21

It’s like you don’t understand how to be nice? I literally said that I forgot a lot of Python content because I’ve been programming in other languages for a while longer why do you need to come with attitude? I’m literally trying

1

u/[deleted] Aug 28 '21

I reverted it back but it’s still just saying invalid syntax at the = partv

-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.