Help with small individual project


#1

I WANT IT TO CHECK FOR A NUMBER OR WORD BY USING .isalpha()

This is the code I have so far

name=raw_input("What's your name?")
print "Hey " + name.title()
feeling=raw_input("Are you having a good day so far? Yes or No? ")
if feeling== "yes":
print "Awesome to hear " + name.title() + "!"
if feeling=="no":
print "Sorry to hear that " + name.title() + ":("
number=raw_input("Do you prefer numbers or words? ")
print "Ahh " +number + " huh?"
if number=="words":
print "Whats your favorite word?"
if number=="numbers":
print "Whats your favorite number?"
def number(k):
"""Trying so hard rn"""
numberorword=word
print "%d ohh cool %d." % (word, number)
return number
if len("Do you prefer numbers or words?") ==0 and number.isalpha():
print "Thats an awesome number"
else:
print "Thats not a number"

I want to make my code check whether the user is following directions properly when the code asks what their favorite number is. If it is a real number, it prints "That is an awesome number". If it is something other than a number, it prints "Thats not a number"

Hope I did a well enough job explaining, I'm only a few days into coding and am learning as I go.


#2

Hey, @pvmsupra !
Just a few things about your code:

if number=="words":
print "Whats your favorite word?"
if number=="numbers":
print "Whats your favorite number?"

That segment of code won't do much other that compare the values, in this case strings. You'll just print it out a message and when the user thinks he/she will type something, the code ends.
If you want to get something from the user, use raw_input(), just like you did in a few lines of code before those ones.

if len("Do you prefer numbers or words?") ==0 and number.isalpha():
print "Thats an awesome number"
else:
print "Thats not a number"

You're comparing the length of the string "Do you prefer numbers of words?", other than the input from the user.
I'd suggest using that after you get the persons name, so you'll know what they want to do.

def number(k):

This is not incorrect but you're passing an argument to this function and so far you didn't call it on your code. If you're not familiar with functions, try making a code without it but if you really want it, try it without an argument, that thing between the parentheses, because you're simply not using the argument.

@pvmsupra, you're doing a really good job, seriously! You just need to fix a few things and your code will execute perfectly!

If I wasn't clear at something, let me know and I'll find another way to explain!

Keep up the good work!! :+1:

Edit:

number.isalpha():

This segment of code will check if the value stored in number is only composed by characters. It seems on that statement if the condition is True, it will print a message saying it's a number but what would you do if it was "ABCD"?
That's not a number! Just change the message of it or simply ask if number.isalpha() == False (it would be False if you had numbers mixed with characters or simply characters stored on the variable number)


#3

Thank you so much for your response! It took me a little bit to reevaluate my code but thanks to your input i realized i could change print to raw_input for the numbers and words portion of it. Here is my new code

name=raw_input("What's your name?")
print "Hey " + name.title()
feeling=raw_input("Are you having a good day so far? Yes or No? ")
if feeling== "yes":
print "Awesome to hear " + name.title() + "!"
if feeling=="no":
print "Sorry to hear that " + name.title() + ":("
numberorword=raw_input("Do you prefer numbers or words? ")
print "Ahh " +numberorword + " huh?"
if numberorword=="words":
word=raw_input("Whats your favorite word?")
if word.isalpha()==True:
print "Thats not a word"
if word.isalpha()==False:
print "Thats an interesting word"

if numberorword=="numbers":
number=raw_input("Whats your favorite number?")
if number.isalpha()==True:
print "Thats not a number"
if number.isalpha()==False:
print "Thats a cool number"

I almost have it exactly as I was hoping to get it but I can't seem to figure out a way of seperating the .numbers from .words... For example if I choose "Words" it throws the response

Do you prefer numbers or words? words
Ahh words huh?
Whats your favorite word? blue
Thats not a word
Thats a cool number

It is vise versa for the numbers portion...

I was thinking about using a def to separate the two prompts but am still not sure if that is the cleanest way to do it, nor would I really know how to start that up... lol I have some ideas but am trying to avoid shooting my brains out figuring it out if there is a cleaner/simpler way :o


#4

@pvmsupra:

if number.isalpha()==True:
print "Thats not a number"
if number.isalpha()==False:
print "Thats a cool number"


The problem is you'll check if the value stored in the variable number is only composed by characters or not in both if statemens.
If the value contains only character then it will be enough to simply check if number.isalpha() == True, if it turns out it's false, there won't be a need to do another if but an else so the first instruction can be executed, if the logic value from the comparison is True, or the second instruction can be executed in case the first one doesn't execute.

If I wasn't clear, let me know and I'll find another way to explain.


#5

Just saw your email! And thank you so much! I think I understand what you're saying, one of statement should be enough to cover both replies.. And I think I know exactly how to do it. Just got to work so I'm going to try it once I get home, dude thank you so much for your help! I'm really excited to see how it pans out when I get home. I'll let you know how it goes! :grin: