I have tried to run one simple code for enter the user input and print that value like below :
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Now when i run this code then its only provide the option to enter the first input like below:
Enter first number:
once i input the number in this line then nothing to happened and also not showing the next one line for the input.
Can you please let me know if i missed anything here? or any idea please share.
Related
I would like the following code to ask the user -in a while loop- to input two integers, handle ValueError Exception, and if all well to print the sum.
My problem is that if the first number passes and the second doesn't, it asks to input the first one all over again.
How do I get it in this case to prompt the second input only?
while True:
try:
number_1 = int(input("enter the first number: "))
number_2 = int(input("enter the second number: "))
except ValueError:
print("please enter numbers only!")
else:
result = number_1 + number_2
print(f" {number_1} + {number_2} = {result}")
Many thanks in advance!
Probably writing a small function to request user for input and handle the ValueError would come handy in here and would be a good practice to use.
Example:
def get_input(show_text:str):
while True:
try:
number = int(input(show_text))
break
except ValueError:
print('Enter number only!')
return number
number_1 = get_input('enter the first number: ')
number_2 = get_input('enter the second number: ')
result = number_1 + number_2
print(f" {number_1} + {number_2} = {result}")
Example output
enter the first number: 1
enter the second number: a
Enter number only!
enter the second number: f
Enter number only!
enter the second number: 2
1 + 2 = 3
Hi I am a newbiew learning python and i have a very simple question and can't seem to work it out.
I have built this little program and i was just wondering one thing.
print("Hello Sir !!!")
num1 = input("Enter a number: ")
num2 = input("Enter another Number: ")
result = float(num1) + float(num2)
print(result)
num3 = input("Enter your final Number: ")
result = float(num3) / (float(num1) + float(num2))
print("Your final total is:", result)
print("You are now finished")
print("Have an Amazing day!! ")
RESULT =
Hello Sir !!!
Enter a number: 50
Enter another Number: 50
100.0
Enter your final Number: 5
Your final total is: 0.05
You are now finished
Have an Amazing day!!
Process finished with exit code 0
If i wanted to write "Your final total is:0.05" or "Your final total is:
0.05"
How would i move it closer or further away?
Thank you for your help today
If you want to add more whitespaces, you can just added it inside the string. If you want a new line, you can use "\n" in your string, which indicate the start of a new line.
Check this link to find out more about escape characters:
https://www.tutorialspoint.com/escape-characters-in-python
You can do
print("Some string: " + variable) # Add a whitespace at the end of your string
if you have string with variable or
print(variable1 + ' ' + variable2)
If you need space between 2 variables or use \n if you need to make a newline
Let's say you have the following code:
def statementz():
print("You typed in", number)
digits = {
56 : statementz
}
while True:
try:
number = int(input("Enter a number: "))
except TypeError:
print("Invalid. Try again.\n")
continue
else:
digits.get(number, lambda : None)()
break
I am wondering if there is a way so that one could allow the dictionary to trigger the "statementz" function if the variable "number" holds the value of any integer/float, and not just the integer that is given in the (rather sloppy) example above.
Is it possible to do this? Thank you in advance for any guidance given!
If you want any number to be passed to statementz(), you can simply call it directly after validating it as a number. A dict is only useful if you want to map different numbers to different functions, which is not the behavior you are asking for. Also note that your statementz() should take a number as a parameter since you want to print the number in the function:
def statementz(number):
print("You typed in", number)
while True:
try:
number = float(input("Enter a number: "))
statementz(number)
except TypeError:
print("Invalid. Try again.\n")
Without try-except:
Try using:
def statementz(number):
print("You typed in", number)
while True:
number = input("Enter a number: ")
if number.isdigit():
statementz(number)
break
else:
print("Invalid. Try again.\n")
Example Output:
Enter a number: Apple
Invalid. Try again.
Enter a number: 12ab
Invalid. Try again.
Enter a number: --41-
Invalid. Try again.
Enter a number: 24
You typed in 24
I want the user of my program to provide a number and then the user must input that number of numbers.
My input collection code
inp1 = int(raw_input("Insert number: "))
inp2 = raw_input("Insert your numbers: ")
Example:
If the user enters 3 at Insert number: then they have to input three numbers (with spaces between them) at Insert your numbers:.
How do I limit the number of values in the second response to the amount specified in the first response?
I assume, we should use a list to work with.
my_list = inp2.split()
I'm using Python 2.7
Use the len function to get the length of the list, then test if it is correct:
inp1 = int(raw_input("Insert number: "))
inp2 = raw_input("Insert your numbers: ").split()
while len(inp2) != inp1:
print "Invalid input"
inp2 = raw_input("Insert your numbers: ").split()
Another approach would be to get each input on a new line seperately, with a loop:
inp1 = int(raw_input("Insert number: "))
inp2 = []
for i in range(inp1):
inp2.append(raw_input("Enter input " + str(i) + ": "))
This way, there are no invalid inputs; the user has to enter the right amount of numbers. However, it isn't exactly what your question asked.
I'm new to python. I was creating a code that use .isdigit. It goes like this:
a = int(input("Enter 1st number: "))
if 'a'.isdigit():
b = int(input("Enter 2nd number: "))
else:
print "Your input is invalid."
But when I enter an alphabet, it doesn't come out the "Your input is invalid.
And if I entered a digit, it doesn't show the b, 'Enter 2nd number'.
Is there anyway anyone out there can help me see what's the issue with my code.
That will be a great help. Thanks.
You are assigning the input to a variable a, but when you try to query it with isdigit() you're actually querying a string 'a', not the variable you created.
Also, you are forcing a conversion to an int before you've even checked if it's an int. If you need to convert it to an int, you should do that after you run the .isdigit() check:
a = raw_input("Enter 1st number: ")
if a.isdigit():
a = int(a)
b = int(raw_input("Enter 2nd number: "))
else:
print("Your input is invalid.")
Try to convert your a variable to string type, like this:
a = int(input("Enter 1st number: "))
if str(a).isdigit():
b = int(input("Enter 2nd number: "))
else:
print("Your input is invalid.")