I'm trying to create a program that asks for the user's input then returns the information with a statement. This code below just returns a blank statement. Any help will be greatly appreciated. I am using python idle.
gnc = input ("Enter gnc here: ")
if input == "masculine singular nominative":
print ("qui")
I suppose you need to print only if it's a valid input (not null)
You have to check if the input value is not empty or just spaces. For that you need to trim the extra spaces in your input and if the condition satisfies you need to print!
gnc = input("Enter gnc here: ")
if str(gnc).strip():
print("Value of gnc is %s", str(gnc))
else:
print("gnc is empty")
Your comparison of gnc will not be true unless the user enters two spaces when prompted. Try the following, and the gnc will be available in the variable gnc.
gnc = input ("Enter gnc here: ")
if not gnc:
print ("gnc is empty")
print("The value of gnc is: {}".format(gnc))
easier:
gnc = raw_input("Enter gnc here: ")
if not gnc:
print ("Empty")
print gnc
Related
I'm a newbie in coding using python and I'm trying to write a piece of small code that prints whatever you input in the terminal, but I don't want anything that only includes spaces and no other characters or doesn't even have an input to be printed. Here's the code:
while True:
my_name= input("> ")
if "" in my_name:
print("I don't know what that means. Please choose a valid answer.")
else:
print(f"Ah, I see, so your name is {my_name}?")
would love to have multiple solutions to this, and thank you if you helped :)
https://docs.python.org/2/library/stdtypes.html#str.isspace
I think this should do the trick
if my_name.isspace():
print("Your name is full of spaces")
Python isspace() method is used to check space in the string. It returns true if there are only white space characters in the string. Otherwise it returns false.
ex:
# Python isspace() method example
# Variable declaration
str = " " # empty string
# Calling function
str2 = str.isspace()
# Displaying result
print(str2)
There's one way of doing that by using exceptional handling for no input using try - except block and a flag variable that checks if input doesn't contain only spaces.
try:
flag = False
my_name = input()
for i in my_name:
if i != ' ':
flag = True
if flag:
print(my_name)
else:
print('Invalid input')
except:
print('No input')
The other ways can be using the built in function strip() which removes the trailing and leading spaces in input.
If the input has only spaces then strip() will remove all of them resulting in an empty string.
try:
my_name = input()
if my_name.strip() == '':
print('Invalid input')
else:
print(my_name)
except:
print('No input')
Like this, you can use bools:
while True:
my_name= input("> ")
if my_name.strip():
print(f"Ah, I see, so your name is {my_name}?")
else:
print("I don't know what that means. Please choose a valid answer.")
I cannot get the code below to work properly. It works if the user enters numbers for the name and it prints the theName.isdigit. But if the user enters both numbers and letters, it accepts this and moves onto a welcome message that follows. Looking at this, is there a reason you can find why theName.isalnum is not working here but the one above is?
theName = raw_input ("What is your name?? ")
while theName.isdigit ():
if theName.isdigit ():
print "What kind of real name has just numbers in it?? Try again..."
elif theName.isalnum ():
print "What kind of name has any numbers in it?? Please try again..."
elif theName.isalpha ():
print "Ok, great"
break
theName = raw_input ("What is your name?? ")
theName = raw_input ("What is your name?? ")
while not theName.isalpha ():
if theName.isdigit ():
print "What kind of real name has just numbers in it?? Try again..."
elif theName.isalnum ():
print "What kind of name has any numbers in it?? Please try again..."
theName = raw_input ("What is your name?? ")
print "Ok, great"
The while condition should tell you when to stop looping, that is, when the input isalpha. Then, because the while loop stops when the input is correct, you can move the logic for what to do in that case below the loop.
Looping on isdigit is problematic because the string abc123 doesn't meet that condition, so you break out of the loop even though the name doesn't meet your criteria.
As mentioned by others your code has a few problems.
First, if the theName contains anything other than digits, you will never enter the while loop, because isdigit() will return False.
Next, the order of your tests means that you will only reach the isalpha() test if the entered name contains something other than letters or digits.
However, it is also overly complex. Assuming your goal is to get the user to enter a name consisting only of letters (i.e. no spaces, digits, or special characters)
theName = "1" # preseed with invalid value
firstTime = True
while not theName.isalpha():
if not firstTime:
print "Your name should not contain anything other than letters"
theName = raw_input("Please enter your name: ")
firstTime = False
print "OK, great. Hi " + theName
This will repeatedly prompt until the user enters a valid name.
I am currently writing some python code that asks for a users name. I need the program to validate whether the user's input is a string or not. If it is a string, The loop will break and the programme will continue. If the input is not a string (like a float, integer etc), It will loop around and ask the user to input a valid string. I understand that you can use the following code when you are checking for an integer;
while True:
try:
number = int(input("Plese enter a number: "))
break
except ValueError:
print("Please enter a valid integer")
I was thinking that I could use something like this to check for a string;
while True:
try:
word = str(input("Plese enter a string: "))
break
except ValueError:
print("Please enter a valid string")
But as far as I understand, the str() just converts the user's input to a string, regardless of the data that was entered.
Please help!
thanks
Based on the comments, it looks like you want to check if the input consists of alphabetic characters only. You can use .isalpha() method:
while True:
word = input("Please enter a string: ")
if word.isalpha():
# Do something with valid input.
break
else:
print("Please enter a valid string")
anwser=str(input("Do you need a new phone? "))
if answer== "no":
print ("You are now finished. ")
else:
question1=str(input("Do you know what phone you want? ")
if question1== "no":
print("Research different phones and chose which pne you like best.")
else:
question2=str(input("Do you want to go on a contract? ")
if question2== "no":
question3=str(input("Do you have enought money to pay full price for your phone? ")
What is wrong? How do I improve? It keeps coming up with a syntax error and I don not know why.
You're missing closing parentheses on your question lines:
question1 = str(input("Do you know what phone you want? ")
Should be:
question1 = str(input("Do you know what phone you want? "))
You also don't need to convert the input to a string, because input() already does that for you:
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that.
So for an exam question I've followed this specific pseudo code which basically makes a program which encrypts a number sequence using the same principle as the ceasar cipher. It should work but for some reason it returns the error.
TypeError: 'int' object is not iterable
Heres the code, i hope you guys can help me, much appreciated
plainNum = input("enter a number to encode ")
codedNum = ' '
Key = input("enter a key ")
for i in plainNum:
codedNum = codedNum + str((int(i)+key)%10)
print codedNum
Use raw_input if you expect a string:
plainNum = raw_input("enter a number to encode ")
input() interprets the input as if it is Python code; enter 5 and it'll return an integer, enter 'some text' (with quotes) and it'll return a string. raw_input() on the other hand returns the entered input uninterpreted.
Most dirty fix of all, simply change
for i in plainNum:
with
for i in str(plainNum):
This is working but not if I use a decimal and it doesn't behave if I enter words or spaces. Consider checking first that the entry is a number with something like:
try:
float(element)
except ValueError:
print "Not a float"
after stripping any whitespace with something like:
plainNum = plainNum.strip()
But this outputs the encoded digits of your entered integer:
plainNum = raw_input("enter a number to encode ")
codedNum = ' '
key = input("enter a key ")
for i in plainNum:
codedNum = codedNum + str((int(i)+key)%10)
print codedNum
Ask the user for the number with raw_input. This makes the input a string which you can iterate over with:
for char in plainNum:
Yes, this is a now a char in a string and so you've used the int(i) function.
you maybe also wanna change key to Key to reflect what variable is declared
and also make codeNum initially equal to '' instead of ' ' (no space vs space)
just book keeping stuff