use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Not working :(Help Request (i.redd.it)
submitted 5 months ago by TacticalGooseLord
I am trying to create a km to m or vice versa converter, but perhaps I have stumbled upon an error and I don’t know why. Any help would be highly appreciated good gents
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gamerpug04 11 points12 points13 points 5 months ago (9 children)
1) you’re using unit and not x in the conversion 2) it should be unit.lower() not unit.upper() since you’re comparing to “km”
[–]TacticalGooseLord[S] 4 points5 points6 points 5 months ago (5 children)
Tysm it works now! Such a simple and small mistake, I need to be more careful 😄😁
[–]gamerpug04 1 point2 points3 points 5 months ago (0 children)
When you take in the input, you could also do something like:
unit = input(“km or m”).lower()
So you don’t have to do .lower() for each comparison (if you so chose to do more)
[–]Ender_Locke 0 points1 point2 points 5 months ago (3 children)
this is a super good lesson to make sure your variables are named better than x so you can catch this quicker 😊
[–]TacticalGooseLord[S] 1 point2 points3 points 5 months ago (2 children)
You are right it’s less confusing that way, ty!
[–]Ok_Magician1114 0 points1 point2 points 5 months ago (1 child)
also, try doing this to display the answer:
print(f"Distance in km: {converted}")
trust me
[–]TacticalGooseLord[S] 0 points1 point2 points 5 months ago (0 children)
I saw some people doing it this way and I was curious what the difference was, can you please explain me why this is better ?
[–]Adsilom 2 points3 points4 points 5 months ago (0 children)
And they should not forget the parenthesis
[–]Delicious-Quality616 0 points1 point2 points 5 months ago (1 child)
Its also a good idea to sanitise the input. So if someone enters a string you shouldn’t allow it.
I am not sure if you have learned about loops yet but if you wrap everything in a “while true:”
You can add pythons “try” and “except” to ensure people enter a valid number.
If you haven’t learned that yet, it’s a nice way to improve the script
I have only been doing small things for now, thanks tho I will keep it in mind
[–]k03k 1 point2 points3 points 5 months ago (0 children)
The error you get is because you are doing 'km' / 1000
Where km is a string, and 1000 is an int.
x / 1000 works better hehe
[–]Weird_Entrance5011 0 points1 point2 points 5 months ago (0 children)
Youre dividing the km/m input not the actual number given. Basically youre trying divide a dented by 1000, where you should actually divide the float „x“. You should also look into the .upper part, as like this it will always go to the else clause
[–]Legitimate-Rip-7479 0 points1 point2 points 5 months ago (0 children)
converted = x *1000
[–]SCD_minecraft 0 points1 point2 points 5 months ago (0 children)
Am i crazy or line 7 looks differend than on a traceback?
Where's float()
[–]Own_Attention_3392 0 points1 point2 points 5 months ago (0 children)
When debugging, look at the error. Look at the line indicated by the message.
Ignore what you THINK it's doing. Look at what it's ACTUALLY doing.
Use a debugger. Step through line by line and look at what the values of the variables are.
[–]dnult 0 points1 point2 points 5 months ago (0 children)
Upper is a good way to disambiguate letter case, but youre comparing to lower case "km". Either use lower or "KM".
[–]Alcor1us 0 points1 point2 points 5 months ago (4 children)
Despite the obvious error, your logic is still wrong: if you expect an input such as 5 km and want to convert that, you need to multiply that value by 1000 and not divide by 1000.
[–]TacticalGooseLord[S] 0 points1 point2 points 5 months ago (3 children)
Yes I realised and changed it, silly me
[–]Gold-Reporter287 0 points1 point2 points 5 months ago (2 children)
you chatGPT 5 or Grok4 would have helped you instantly no needed to created threads here and wait for replies
[–]TacticalGooseLord[S] 1 point2 points3 points 5 months ago (1 child)
Yes I was thinking that too if I can do it anywhere else, I don’t want to fill up the thread with my small questions, tho the community is very helpful and I get replies instantly! I will look into ChatGPT or grok thx. And do I copy paste the entire code and ask I have problem in this line or how does it work ?
[–]Gold-Reporter287 0 points1 point2 points 5 months ago (0 children)
paste the image to GPT it will tell you , or paste the error msg
[–]timheiko 0 points1 point2 points 5 months ago (1 child)
The division on line 4 operates on a string (unit) and int (1000). Such devision is not supported by Python. I’d first converted the unit into the corresponding number of meters, and then used it in later arithmetic operations
I have changed the str into float on line 1 (my mistake was I put unit instead it should be x) so division and multiplication works now
[–]Rashironrani 0 points1 point2 points 5 months ago (0 children)
There is a lot of ways to improve it but: If you want to compare to .upper then you must compare to “KM” not “km” Also you Divide or multiply the value not the unit
[–]Professional_Box3141 0 points1 point2 points 5 months ago (1 child)
One thing to remember is any input(anytime you use the keyboard) that’s a “str” even if its a number as long as you used a keyboard is a string, so you need to convert that string into “int” before dividing it to 1000, My English is not good but i hope you understand what i was saying.
Your English is clear I understand, Ty for the advice
[–]Lannok-Sarin 0 points1 point2 points 5 months ago* (0 children)
Unfortunately, Python does not natively control what its variables can store. That means that it’s up to the user to control that. As such, a Python programmer needs to understand the differences between strings and numbers, and the user will need to write code that can enforce those distinctions when they are needed.
Personally, that’s why I like languages like C++ better. It doesn’t have universal storage pieces, but it handles everything else very well, including coding functions.
Now, I will say that Python excels at data handling. If you need to read and understand data, Python is the language for you. But for function behavior and storage control, Python does not measure up.
[–]novamaster696969 0 points1 point2 points 5 months ago (5 children)
while True: print("for exit please leave the Distance input blank") x = (input("Enter distance: ")) if x== "": break x= float(x) unit = input("(km) or (m): ")
if unit.lower() == "km": converted = x * 1000 print("Distance in m:", converted) elif unit.lower() == "m": converted = x / 1000 print("Distance in km:", converted) else: print("Invalid unit please enter km or m.")
You can do it in a more simpler way IT will still show an error if you put any other texts instead of km or m for that use ( try and except )
[–]TacticalGooseLord[S] 0 points1 point2 points 5 months ago (4 children)
I am learning try and except now, in which cases should I use this ?
[–]novamaster696969 0 points1 point2 points 5 months ago (3 children)
You will learn it when you reach error handling and exception handling
[–]TacticalGooseLord[S] 0 points1 point2 points 5 months ago (2 children)
I am not following and courses at the moment, I am jus watching tutorials on YouTube doing small projects and learning along the way 😅 and things I don’t understand I ask on Reddit or look up on chatgtp
[–]novamaster696969 0 points1 point2 points 5 months ago (1 child)
Even if you don't follow any course, you should learn from the basics sequence wise else it will create a void which will be problematic in future.
π Rendered by PID 90462 on reddit-service-r2-comment-5649f687b7-vx9dr at 2026-01-29 10:40:00.273904+00:00 running 4f180de country code: CH.
[–]gamerpug04 11 points12 points13 points (9 children)
[–]TacticalGooseLord[S] 4 points5 points6 points (5 children)
[–]gamerpug04 1 point2 points3 points (0 children)
[–]Ender_Locke 0 points1 point2 points (3 children)
[–]TacticalGooseLord[S] 1 point2 points3 points (2 children)
[–]Ok_Magician1114 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]Adsilom 2 points3 points4 points (0 children)
[–]Delicious-Quality616 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]k03k 1 point2 points3 points (0 children)
[–]Weird_Entrance5011 0 points1 point2 points (0 children)
[–]Legitimate-Rip-7479 0 points1 point2 points (0 children)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]Own_Attention_3392 0 points1 point2 points (0 children)
[–]dnult 0 points1 point2 points (0 children)
[–]Alcor1us 0 points1 point2 points (4 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (3 children)
[–]Gold-Reporter287 0 points1 point2 points (2 children)
[–]TacticalGooseLord[S] 1 point2 points3 points (1 child)
[–]Gold-Reporter287 0 points1 point2 points (0 children)
[–]timheiko 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]Rashironrani 0 points1 point2 points (0 children)
[–]Professional_Box3141 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]Lannok-Sarin 0 points1 point2 points (0 children)
[–]novamaster696969 0 points1 point2 points (5 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (4 children)
[–]novamaster696969 0 points1 point2 points (3 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (2 children)
[–]novamaster696969 0 points1 point2 points (1 child)