I want the while loop to run as long as the name is not "1234". And if the name is only spaces, I want to print "name can not be only spaces" and ask for a name again. Else I want it to just ask for a name again.
name=input("")
while (name!="1234"):
if(name.isspace):
print ("name can not be only spaces")
name=input("")
else:
name=input("")
The problem I have is that it prints "name can not only be spaces" for whatever string I give it. Even if the string does not have spaces it prints the if statement. What is going on?
You might just be missing the parentheses of the isspace function.
So turn isspace into isspace() and you should be fine.
As Mat has already mentioned, the problem you're facing is simply because name.ispace does not return a boolean like you need in your program.
Also, I think you could simplify your code a bit -
name=input("")
while (name!="1234"):
if(name.isspace()):
print ("name can not be only spaces")
name=input("")
Related
For example, if I have the following line:
"I want to remove first five words and I do not know how to do that."
And I want to change the line to :
"five words and I do not know how to do that."
I am wondering if there is some built-in function to help me to do that and thanks!
a = "I want to remove first five words and I do not know how to do that."
print ' '.join(a.split()[5:])
This is part of a school project. I cannot figure out what's the problem in my regexes. I have more that work but these are giving me a hard time. Apache doesn't tell you exactly where you went wrong.
First and Last name must be two simple names and output in Lastname, Firstname format
my $name = param('name');
if($name =~ {2}) {
print "Name will be stored as $2, $1<br/><br/>";
} else {
print "Bad name. Enter just two names, your first and last<br/><br/>";
}
Password must be in this order of regexes. Begin with a single upper case character, 2 digits, a single space, 2-3 lower case letters, one special character (not a letter or digit).
my $password = param('password');
if ($password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+-]\^$/) {
print "Password $password was accepted<br/><br/>";
} else {
print "Bad password, $password was not accepted<br/><br/>";
}
Apache doesn't tell you exactly where you went wrong.
First, find your Apache error log. It will contain the actual error. I can't tell you where it is, but I'd start with /var/log.
Second, debugging code through a web server just makes things more difficult. You're probably using CGI.pm which can accept arguments on the command line for debugging.
perl /path/to/your/program name='Michael Schwern'
Second, turn on strict and warnings. They will point out typos and silly mistakes like this one...
$ perl -w ~/tmp/test.plx name=foo
Odd number of elements in anonymous hash at /Users/schwern/tmp/test.plx line 5.
Bad name. Enter just two names, your first and last<br/><br/>
That's this.
$name =~ {2}
That says to make an anonymous hash with the key 2 and an undefined value. Then stringify it to something like HASH(0x7fca01805668) and then use that as a regex. In other words: nonsense.
What you're looking for is something like this that looks for two words separated by some spaces.
$name =~ m{^(\w+)\s+(\w+)$};
Read the Perl regex tutorial for more info.
It's hard to tell what you're trying to do, and getting other people to do your homework for you won't teach you a thing
$name =~ {2}
isn't a regular expression at all: you're building an anonymous hash { 2 => undef } and using its stringified reference as a pattern. It will be something like HASH(0x71c328) so that isn't going to work
And this one is incomprehensible
$password =~ /[A-Z]+\d{2}+\s+[a-z]{2,3}+-]\^$/
It will match something like A99 aaa-]^, but I doubt if that is what you want. What is the plus sign + for after {2,3}?
I'm an absolute beginner trying to learn string validation. I have a variable about to store user input:
Text_input = raw_input('Type anything: ')
I want to check if Text_input contains at least one alphanumeric character. (If not, the program should print a message such as "Try again!" and ask the user to type again.) So, typing "A#" should pass but "#" should not. Any suggestions?
This worked for me:
Text_input = raw_input('Type anything: ')
if any(char.isalpha() or char.isdigit() for char in Text_input):
print "Input contains at least one alphanumeric character."
else:
print "Input must contain at least one alphanumeric character."
Very new to the world of programming and just starting to learn, working through Flatiron School prework and have been doing ok but unable to understand "if" and "else" statements for some reason. The problem is similiar to Chris Pine 'deaf grandma' problem but without saying "BYE!" three times.
~The method should take in a string argument containing a phrase and check to see if the phrase is written in all uppercase: if it isn't, then grandma can't hear you. She should then respond with (return) HUH?! SPEAK UP, SONNY!.
~However, if you shout at her (i.e. call the method with a string argument containing a phrase that is all uppercase, then she can hear you (or at least she thinks that she can) and should respond with (return) NO, NOT SINCE 1938!
I have so far:
def speak_to_grandma
puts "Hi Nana, how are you?".upcase
if false
puts "HUH?! SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE 1938!"
end
end
but am getting wrong number of arguments...how am I supposed to add argument while using the if/else statements? This is probably a very easy and basic question but can't seem to get my head around this (overthinking probably).
Any help and clarity would be greatly appreciated.
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if string equals same phrase all upper case letters, which means string is all uppercase
if phrase == phrase.upcase
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
else
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
end
end
# execute function passing input_phrase variable as argument
speak_to_grandma(input_phrase)
how am I supposed to add argument while using the if/else statements?
This is probably a very easy and basic question but can't seem to get
my head around this (overthinking probably).
Your mistake was that function was not accepting any arguments, here it accepts "phrase" variable as argument and processes it:
def speak_to_grandma(phrase)
You had
if false
but did not check what exactly is false.. To rewrite my version with "false" :
input_phrase = "Hi Nana, how are you?"
def speak_to_grandma(phrase)
# Check if it is false that string is all upper case
if (phrase == phrase.upcase) == false
# return this string if condition is false
puts "HUH?! SPEAK UP, SONNY!"
else
# return this string if condition is true
puts "NO, NOT SINCE 1938!"
end
end
speak_to_grandma(input_phrase)
Here I am evaluating
if (phrase == phrase.upcase) == false
Basically means "if expression that phrase equals phrase all uppercase is false"
My python code snippet is some what like this
CLName = raw_input ("\n\nEnter 7 digits CL number !")
if os.path.isdir("\\\\abc\\\Compiler_Builds\\\something\\\CLName"):
print "CL Exists"
\\\\abc\\Compiler_Builds\\something is a local windows server location. In the above code I want os.path.dir() to check if the path \\\\abc\\\Compiler_Builds\\\something exists by inserting CLName at end, which will be given by user. I have tried to be as specific as i could. Please let me know if i need to provide more info on this and also please ignore any grammatical or English mistakes.
CLName = raw_input ("\n\nEnter 7 digits CL number !")
if os.path.isdir("/abc/Compiler_Builds/something/%s" % CLName):
print "CL Exists"
You interpolate CLName into the string, as shown.
You don't need to use escaped backslashes, just use forwards slashes.