Python Forum
I'm not sure what I'm doing wrong here...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm not sure what I'm doing wrong here...
#1
Hey all, beginning programmer here.

I've been working on a very simple Mad Libs-style "game" in python 2.7 and I've run into some trouble when trying to execute it. 


The program has five variables: a number (number1), a verb (verb1), a color (color1), a noun (noun1), and a name (name1). The program asks you to fill them in, in that order, and they appear in the following generated story in that order (or at least they're supposed to).

Here is the empty sentence without any of the variables filled in: "I saw <number1> <color> <verb1> <noun1> the other day. It said to me, 'Hi, my name is <name1>.'"
Here is the sentence with the variables filled in as an example: "I saw one green hopping bird the other day. It said to me, 'Hi, my name is Frank.'"

Seems simple right? Well it gets a little more complicated.

Basically the program accounts for a value greater than 1 being assigned to number1. The reason why this is important is because if number1 > 1 but noun1 <= 1, the sentence becomes grammatically incorrect. "I saw ten green hopping bird the other day. It said to me, 'Hi, my name is Frank.'" Hopefully you see what I mean. It's also worth mentioning that the sentence changes from "It said to me" to "The first one said to me" if number1 > 1.

Therefore I (tried) implementing a system in which the program checks whether or not number1 is plural, and if it is, it changes the raw_input of noun1, as you can see on line 17. The "default" raw_input command for noun1 would be raw_input("Choose a noun.") However if the program detects number1 > 1, it changes to raw_input("Choose a plural noun.") and if number1 <= 1 it becomes raw_input("Choose a singular noun."). So long as the user follows the instructions the sentence should be grammatically correct and thus flexible to both plural and singular subjects. 

I also have a final_product variable that is the sentence, and I'm using %s to fill in the blanks with the player input as you can see on line 30. 

Hopefully all of this makes sense.

So here's the problem: when I execute the code, wherein
number1 = one
verb1 = hopping
color1 = green
noun1 = bird
name1 = Frank

I get this:
"I saw <function number1 at 0x03039930> hopping green <function noun1 at 0x03039970> the other day. The first one said to me, 'Hi, my name is Frank.'"

There's not even any error messages or anything, it's just that for some reason he values for number1 and noun1 aren't filled in. Furthermore the program didn't ask me for a noun (singular or plural), it just skipped over to name. And the sentence doesn't change from "The first one said" to "It said" based on the number1 input.

I have no clue how I'm supposed to solve this. I hope some more experienced folk can help me out and I'd be very grateful for it.
Reply
#2
For starters:

Your number1 code
number1 = raw_input("Choose a number.")
def number1():
    if number1 <= 1:
        number1 = "a"
    elif number1 > 1:
        number1 = number1
Very confusing and potential for error exists (even though technically legal) when variables are named the same as functions
also you don't need the elif it does nothing.Why would you have to make a variable equal to itself?
You should also refrain from using globals, but that's something you will pick up with experience ... just keep it in the back of your mind.
In addition, if you're going to the trouble of naming something 'number'... don't store anything but numbers in that variable
finally, you never call this function, so why is it there? and even if you did, you never look for an 'a'

to execute a function, the syntax is functionname(arguments). I think you are confused about this.
defaults should only be used if they make sense, not as a standard way to call a function.
your final_product is a good example of BAD code.

Try fixing things up a bit, then come back with specific issues.
also, next time post the code here (unless it's mile long)
Reply
#3
(Nov-01-2016, 04:29 AM)Larz60+ Wrote: For starters:

Your number1 code
number1 = raw_input("Choose a number.")
def number1():
    if number1 <= 1:
        number1 = "a"
    elif number1 > 1:
        number1 = number1
Very confusing and potential for error exists (even though technically legal) when variables are named the same as functions
also you don't need the elif it does nothing.Why would you have to make a variable equal to itself?
You should also refrain from using globals, but that's something you will pick up with experience ... just keep it in the back of your mind.
In addition, if you're going to the trouble of naming something 'number'... don't store anything but numbers in that variable
finally, you never call this function, so why is it there? and even if you did, you never look for an 'a'

to execute a function, the syntax is functionname(arguments). I think you are confused about this.
defaults should only be used if they make sense, not as a standard way to call a function.
your final_product is a good example of BAD code.

Try fixing things up a bit, then come back with specific issues.
also, next time post the code here (unless it's mile long)


Actually within the body of the function it compares the function object with the number. If you add
print number1
within the function body, it will print the function object, not the value of the global name number1
Also note that raw_input will return str, not int, so without conversion the global name number1 will hold str anyway.
It looks like function object is always greater than int object and the opposite when comparing with str object or when compare str object with int objects

def foo():
    pass
bar=10**100
bar2='some str'
print foo
print bar
print bar2
print foo>bar
print foo>bar2
print bar>bar2
Output:
<function foo at 0x036470B0> 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 some str True False False
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,626 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,044 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020