r/learnpython • u/Bowsers_disciple • 3h ago
I'm making a small program in python to help with some in game math for Iron Nest
Edit: thank you to u/LushBreaker for giving me the starting point to get this fixed!
altitude = 0
... def nest_Alt():
... distance = float(input("How many Km away is the target? \n"))
... charges = int(input("How many charges are you using? \n"))
... altitude = (distance * 12) / charges
... print(f"Fire at altitude {altitude}")
...
>>>
>>> nest_alt()
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
nest_alt()
^^^^^^^^
NameError: name 'nest_alt' is not defined. Did you mean: 'nest_Alt'?
>>> nest_Alt()
How many Km away is the target?
How many Km away is the target?
How many Km away is the target?
How many Km away is the target?
How many Km away is the target?
12.8
How many charges are you using?
How many charges are you using?
5
Fire at altitude 30.720000000000006
The above is the copy pasted result of my most recent version of the code, and something I noticed is that the prompt is resent for each character in an input.
Separately, how would I make this program into a saved file that I can run later, possibly running the function multiple times without having to close it.
Edit: For clarification, this is python 3.14
1
u/LushBreaker 2h ago
you wrote the input inside the function but python is reading it character by character, thats probably because something on your terminal setup is sending each keypress separately. try running the script in a proper terminal like cmd or terminal app, not inside the python interactive shell
for saving the file, just copy your code to a.py file and add a while loop at the bottom so it keeps asking until you type "exit" or something