Trying to accept only text as input [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Check if multiple strings exist in another string
(17 answers)
Closed 3 years ago.
Trying to accept only text as input in my program, this is a snippet where I'm having trouble.
def askStr(th):
try:
a = str(input(th))
except ValueError:
raise ValueError("You may only enter a string (letters.)")
if "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" in a:
raise TypeError("No. Only enter letters.")
return a
When I enter a number, it raises the error as expected. BUT, when I enter anything else, only letters, it still gives me the error. No idea what to do here.

Try this
def askStr(th):
a = str(input(th))
for c in a:
if c in string.digits:
raise TypeError("No. Only enter letters.")
return a

Related

My python variables are not being defined [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
How to use user input to end a program?
(6 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 22 days ago.
Here's the beginning of my program:
import sys
print("Enter a number when prompted, type \"stop\" to stop.")
x = input("Input: ")
if type(x) == "int":
Higher = x
Lower = x
elif x.lower() == "stop":
print("The program has been stopped.")
sys.exit()
If right after this I try to print the variable Higher, for example, I get this error message: NameError: name 'Higher' is not defined
EDIT: I tried declaring the variables beforehand but that doesn't work either.

Comparision Operators issue [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=a>b
print("Is a greater than b ? ", c )
ISSUE is it showing the opposite output always like the when you enter a greater than b it showing flase and vice versa
your problem is that you compare strings instead of floats
since when you are comparing stings python compares the lexicographic value of the strings, that way "9" is grater then "12357645"
if you convert the input to float that should fix it :)
a=input("Enter the first Number ")
b=input("Enter the next number ")
c=float(a)>float(b)
print("Is a greater than b ? ", c )

How to repeat an if else if else loop according to my preference? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 1 year ago.
This is what I tried and , basically what I was trying to do was that, if a user enters other than 1,2,3,4,6 then it will go back to a=int(input.....)). But for me it keeps giving output as None.
def hi():
while True:
try:
a=int(input("enter the denominator of Pi radian\n"
"(choose from 1,2,3,4,6)\n"
"Enter here:"))
if a <=0 or a>=7 and a!=5:
print("Enter the given digits")
else:
return a
except Exception:
print("enter a valid type")
ant=hi()
print(ant)
You have applied a wrong condition here:
if a <=0 or a>=7 and a!=5:
You're checking a!=5 as a wrong input, when it should be a==5. Also apply or instead of and. Change it to:
if a <=0 or a>=7 or a==5:

Everything appears to be defined as a string? Python [duplicate]

This question already has answers here:
Specify input() type in Python?
(2 answers)
Closed 4 years ago.
I am completely new to programming. However, I just wanna write a simple bit of code on Python, that allows me to input any data and the type of the data is relayed or 'printed' back at me.
The current script I have is:
x = input()
print(x)
print(type(x))
However, regardless of i input a string, integer or float it will always print string? Any suggestions?
In Python input always returns a string.
If you want to consider it as an int you have to convert it.
num = int(input('Choose a number: '))
print(num, type(num))
If you aren't sure of the type you can do:
num = input('Choose a number: ')
try:
num = int(num)
except:
pass
print(num, type(num))
1111 or 1.10 are valid strings
If the user presses the "1" key four times and then Enter, there's no magic way to tell if they wanted to enter the number 1111 or the string "1111". The input function gives your program the arbitrary textual data entered by user as a string, and it's up to you to interpret it however you wish.
If you want different treatment for data in particular format (e.g. if they enter "1111" do something with it as a number 1111, and if they enter "111x" show a message "please enter a valid number") then your program needs to implement that logic.

if condition not working properly inside a function python 3 [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
This is the following code.The test2 function doesnt get invoked why?
Even if i enter 1 the test2 fuction is not called
def test2():
print("here i come")
def test1():
x=input("hey ill take u to next fuction")
if(x==1):
test2()
test1()
x=input("hey ill take u to next fuction")
x will be a string type, not an integer. You should change the if statement to compare the same types (either converting x to int, or 1 to "1"
Because you are comparing a string (your input) with 1 that is a integer.So you need to convert the input to int and then compare it.
Also as it can raise ValueError you can use a try-except to handle that :
def test1():
x=input("hey ill take u to next fuction")
try :
if(int(x)==1):
test2()
except ValueError:
print 'enter a valid number'

Categories