r/learnpython • • 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

0 Upvotes

9 comments sorted by

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

1

u/Bowsers_disciple 2h ago

I can't figure out how to even type the code into either cmd or terminal.

1

u/Bowsers_disciple 2h ago

okay I figured it out!

1

u/Moikle 1h ago

Don't. Save it as a .py file and run that from the terminal

1

u/Bowsers_disciple 2h ago

I'm running into an issue where the window closes after putting in the second input, and doesn't give me time to read the printed output

1

u/Bowsers_disciple 2h ago

I've temporarily solved this issue, while solving my other issue too, by adding a different function that prompts you to type in NEW if you want to redo calculations, or EXIT if you don't want to

1

u/Moikle 1h ago

Don't launch python programs by double clicking them in a file manager.

Run them in a terminal with

python path/to/your/module.py

1

u/Moikle 1h ago

Put 4 spaces in front of each line of code to format it for reddit