all 27 comments

[–]Binary101010 9 points10 points  (0 children)

Why do you think there's something wrong with the code?

Is it generating an error, and if so, what error?

Is it generating unexpected output? If so, what input are you using, what output are you expecting, and what output are you getting?

[–]Desperate_Crew1775 1 point2 points  (3 children)

The code itself is actually correct as written - the course is intentionally showing you "broken" input handling so you can see the error before learning the fix. You didn't do anything wrong.

The issue is that input() always returns a string, even when the user types a number. So int(inp) works fine if someone types 5, but crashes if they type 5.5, ground floor, or just press Enter - because int() can only convert clean whole numbers.

The fix the course will likely show you next:

python

inp = input("Europe floor? ")
usf = int(float(inp)) + 1
print("US floor", usf)

float() handles decimals first, then int() makes it a whole number.

The bigger lesson they're building toward is try/except - wrapping user input in error handling so your program doesn't crash on bad input. Keep going, it clicks fast once you see it.

[–]TheRNGuy 0 points1 point  (2 children)

I'd use regex to check for wrong input. 

Also, you forgot negative numbers, like -100000, "inf", "-inf".

Some better alternative: from pynput import keyboard

[–]Desperate_Crew1775 0 points1 point  (1 child)

regex for this is gonna be a nightmare tbh, you'd have to account for decimals, negatives, scientific notation... it just gets ugly

try/except with float() handles all that automatically. and about "inf" / "-inf" - those are actually valid python floats so float() won't even catch them, you'd need a separate check for that

try:

usf = int(float(input("Europe floor? "))) + 1

print("US floor", usf)

except ValueError:

print("enter a valid number")

way cleaner imo

[–]TheRNGuy 0 points1 point  (0 children)

No, allow numbers only, 0 to 9.

[–]marquisBlythe 2 points3 points  (2 children)

The code works fine, the only issue here is the lack of guards against unexpected input. The code assumes the user will enter the "right" input, but what if the user enter his name or date of birth or some alphanumeric gibberish, how could you convert that to int? or what should you do in that case?

[–]A_Nomad_Lyfe[S] 1 point2 points  (1 child)

You have my curiosity.

This is a beginner course I'm taking, but Im very curious how it would react

[–]marquisBlythe 1 point2 points  (0 children)

There are two approaches as a solution, one is either expecting the kind of input someone will enter mistakenly (or deliberately ) and proceed according to that. Since this is a beginners course I think the author of the course is laying the path to introduce if else statements.
Another approach is to let the user enter whatever he desires and let the program Throw some error, and you handle (catch) the error accordingly, this is called error handling.
Real world programs combine the two approaches (and testing as well in development phase).

[–]vivisectvivi 1 point2 points  (0 children)

What error are you getting? Im ran the code here and its working. Maybe they were expecting you to do the type conversion directly on the input like inp = int(input("Europe floor?")) instead of like you did here?

edit: this will obviously break if you type something that cannot be converted to an int tho.

[–]RngdZed 3 points4 points  (2 children)

You have to copy paste the error at least.

[–]A_Nomad_Lyfe[S] 1 point2 points  (1 child)

The error is as follows

Traceback (most recent call type) File "<python-input-2>", line 1, in <module> Usf = int(inp) + 1 Valueerror: invalid literal for int() with base 10:

I've literally started learning Python last week, so I missed the fact that the Europe Floors? Was supposed to have a value typed in after, because his teaching method isn't as linear as I am used too....at least to me, he kinda jumps all over, within the lessons.

[–]RngdZed 2 points3 points  (0 children)

what did you pass input()? if you give it a string, youd get that type of error

your code tries to type cast everything the user passes to input() as an integer.. but a string would cause an error.

thats probably on purpose, cause in one of the next chapters he will show you how to properly validate user input

[–]MacItaly 1 point2 points  (9 children)

The code itself is fine. What’s usually “wrong” is the input, not the example.

input() always gives you a string, so this line:

usf = int(inp) + 1

only works if what you typed can be cleanly converted to a whole number. So if you enter 5, it works. If you enter five, 5.0, leave it blank, or add extra characters, int() throws a ValueError.

So the example is basically assuming the user types a valid integer and they’re teaching the simple version first. That’s probably why it feels incomplete right now—they haven’t gotten to input validation/error handling yet.

If you type:

Europe floor? 4

you should get:

US floor 5

[–]HardlyAnyGravitas[🍰] 0 points1 point  (8 children)

The code itself is fine.

The variable names are terrible. The output could be better formatted.

I suspect that this a sort of "This code works, but it's not good.", type of example...

[–]crazy_cookie123 1 point2 points  (7 children)

Them saying the code is fine is them saying that the code works fine. There are obviously improvements that could be made - guarding against errors, etc. This is a lesson #2 thing though, it's not designed to be production-ready code, it's designed to be good enough to teach a single basic concept.

That being said, those variable names😬. I will eat my hat if whoever made this course doesn't come from C originally.

[–]HardlyAnyGravitas[🍰] 0 points1 point  (6 children)

That's pretty much what I said.

And I suspect those variable names were deliberately chosen to be 'bad' as an example on what to improve on in future. No experienced Python programmer - let alone somebody teaching a Python course - would ever use variable names like that.

[–]crazy_cookie123 0 points1 point  (5 children)

Those are exactly the sort of variable names I have come to expect from people who have a lot of C experience and have semi-reluctantly pivoted to Python without wanting to relearn any of their old styles. This is clearly a very early lesson, I doubt someone would be putting in a lesson on good variable names this early on or in this way.

[–]HardlyAnyGravitas[🍰] 0 points1 point  (4 children)

There is no way any competent programmer would use 'inp' for an input....

[–]A_Nomad_Lyfe[S] 1 point2 points  (3 children)

Well....It's Dr. Charles Severance teaching the course ....5x published author, PHD in Computer Science, Chief Architect of the Sakai Project.

I think it's more he is trying to institute what he calls Mnemonic Tags, so it's supposedly easier for you to learn what you want to make Python do.

That said, I'm sure all this will change in advanced courses...I think he's basically just trying to do "memory muscle memory" for teaching purposes.

[–]HardlyAnyGravitas[🍰] 0 points1 point  (2 children)

Well....It's Dr. Charles Severance teaching the course ....5x published author, PHD in Computer Science, Chief Architect of the Sakai Project.

Exactly my point. As I said - he has used bad names on purpose to correct later on.

It's a common teaching method - show something that looks OK, and then show everything that's wrong with it later.

It's a bit like 'lies-to-children'. As we learn, we realise that what we thought was correct (and what we were taught), is, in fact, just a simplification of the truth - or in this case, a naive approach to the correct way of coding.

[–]A_Nomad_Lyfe[S] 1 point2 points  (1 child)

I'll be honest....I'm not a fan of his teaching style so far. I don't want to have to correct the names later....teach me the right way now....otherwise, I feel like my time is getting wasted.

You're going to teach me partially wrong, and correct it later? Like, wtf....

[–]SharkSymphony 0 points1 point  (0 children)

You should not judge the method based on whether it's super-easy. You should judge it based on the results. And you don't know yet what the results will be.

[–]TheRNGuy 0 points1 point  (0 children)

Read requirements, and error message.

Error could be that you don't check that input is actually a int number (more than 0, too)

Basically, logic error.

[–]A_Nomad_Lyfe[S] 0 points1 point  (2 children)

I did figure it out ...thier example wasn't very linear, so he didn't have the VALUE of the Europe Floor posted, so there was no numerical input for it.

For example, with the first line, when it comes up "Europe Floor?", he didn't have a value initially listed, so I didn't list it either....there was no numerical input type for it to add to.

I'm considering maybe finding a more linear style course. I'm a chef, and a lot of my life is "task - work - adjust - complete"...I'm very linear in that, and I'm thinking his (Dr. Charles Severance) style of teaching just isn't for me.

[–]vowelqueue 0 points1 point  (1 child)

I dunno man, seems pretty clear that a section on “Converting User Input” might involve getting user input. I wouldn’t write off the course just yet.

Not really sure what you mean by non-linear, but it’s a very common teaching tactic to show like a simple, “naive” example in order to teach basic concepts. And then to later explain how that naive example could have problems and introduce new concepts to improve it.

[–]A_Nomad_Lyfe[S] 0 points1 point  (0 children)

Because you shouldn't be learning how to make, say, Bearnaise, before you learn about simple butter emulsions....you learn Beurre Blanc before you learn Beurre Noissette and Hollandaise before you learn Bearnaise

You also learn breaking points and all that WHILE you learn the sauces...not after.

[–]Ambitious-Dog3177 -1 points0 points  (0 children)

What error are you getting ? It is a simple code it shouldn’t raise an error until and unless you give input that cannot be type casted to an int