Input Function#
The input() function allows us as users to add stuff into our code from the command line (or in case of this website, a popup input box).
The input() function allows us to take in a string and use it in our scripts
name = input("What is your name?")
print(f"Hello {name}!")
age = int(input("What is your age?"))
print(f"half of your age is {age/2}")
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
Cell In[1], line 1
----> 1 name = input("What is your name?")
2
3 print(f"Hello {name}!")
4
File ~/.local/lib/python3.12/site-packages/ipykernel/kernelbase.py:1402, in Kernel.raw_input(self, prompt)
1400 if not self._allow_stdin:
1401 msg = "raw_input was called, but this frontend does not support input requests."
-> 1402 raise StdinNotImplementedError(msg)
1403 return self._input_request(
1404 str(prompt),
1405 self._get_shell_context_var(self._shell_parent_ident),
1406 self.get_parent("shell"),
1407 password=False,
1408 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
Note that above you have to cast numbers into their prefered datatype if you are planning on using numbers as numbers. It may also be worth running a check to see if someone actually puts in numbers when you need numbers:
age = input("What is your age?")
#Try and catch lets you see if something will work or not. If an error occurs, the catch option will enable and finish the code.
#If no error occurs, the catch option will not occur
try:
age=int(age)
print(f"half of your age is {age/2}")
except:
print("Please provide a number for your age")
Please provide a number for your age
#Sandbox Area