So I need to make a python program that looks at your mark and gives you varying responses depending on what it is. However I also need to add a way to stop random text which isn't numbers from being entered into the program. I thought I had found a solution to this but it won't make it it past the first statement to the failsafe code that is meant to catch it if it was anything but numbers. So pretty much what happens is if I enter How can I change it so that it can make it to the code that gives them the print statement that they need to enter a number? asked Dec 16, 2014 at 23:51
5 remove eval and your code is correct:
Just checking for a float will work fine:
answered Dec 16, 2014 at 23:55
1 Is it easier to declare a global value than to pass an argument, In my case it's also gives an error.
#can't comment :) answered Feb 28, 2021 at 10:26
You can simply cae to
"Why not use eval?" you ask, well... Try this input from the user: answered Dec 16, 2014 at 23:55
Reut SharabaniReut Sharabani 29.5k6 gold badges69 silver badges86 bronze badges you can use the String object method called isnumeric. it's more efficient than try- except method. see the below code.
answered Apr 5, 2019 at 8:20
Abdul Hadi 1,1991 gold badge10 silver badges19 bronze badges answered Dec 17, 2014 at 1:25
hariKhariK 2,42211 silver badges16 bronze badges Actually if you going to use eval() you have to define more things.
It's a basically control mechanism for eval(). answered Dec 16, 2014 at 23:59
GLHFGLHF 3,71410 gold badges35 silver badges80 bronze badges How do you accept numbers in Python?Use Python built-in input() function to take integer input from the user. This input() function returns string data and it can be stored in a string variable. Then use the int() function to parse into an integer value.
How do you accept 10 numbers in Python?Let's see how to accept Python list as an input without using the split() method.. First, create an empty list.. Next, accept a list size from the user (i.e., the number of elements in a list). Run loop till the size of a list using a for loop and range() function.. use the input() function to receive a number from a user.. |