Question: Guess program

Page 1 of 1

8 Replies - 7094 Views - Last Post: 14 July 2012 - 05:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=282590&amp;s=82bf6d2af854db6f1e5e83f51592bc6c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 mikelcu

Reputation: 0
• Posts: 2
• Joined: 12-June 12

Question: Guess program

Posted 12 June 2012 - 05:53 PM

Hello. I am new in the forum. I just wrote this guess program using and empty list. The user fill the list with 5 items then shuffle the list. User must enter the position in the list to guess, program keeps running until guess is correct. Just wondering if is a nicer way to do it. I am new programming and just started with Python, really love it....I dream with it..Thanks. Miguel
```#----------Guess Game---------
#Entering five items into a list then the user guess the item position in the list
#once the list is been shuffle.....

from random import shuffle

my_list=[]
guess=[]

## Filling the list with user input
for item in range(5):
userinput=raw_input("Enter name:\n")
my_list.append(userinput)
shuffle (my_list)

## Starting guessing the order...
## Enter the position you want to guess

pos = input("Enter the position in the list you want to guess.\nPositive or Negative Number form 1 to 5\n")
pos=int(pos)

while guess!=my_list[pos]:
guess=raw_input('Enter the guess...\n')
if my_list[pos]==guess:
print 'OK'
else:
print 'Wrong'
print my_list

```

Is This A Good Question/Topic? 0

Replies To: Question: Guess program

#2 atraub

• Pythoneer

Reputation: 760
• Posts: 2,015
• Joined: 23-December 08

Re: Question: Guess program

Posted 15 June 2012 - 07:16 PM

Your first problem is, how does the user even know what's going on? I ran your program, and it said "Enter Name" several times without any indication of what's going on. What name should I be entering? My name? A friend's name? A turtle's name? I'm already confused. You need to explain to the user what he's doing.

To add to the confusion, look at this output:
```Enter name:
a
Enter name:
b
Enter name:
c
Enter name:
d
Enter name:
e
Enter the position in the list you want to guess.
Positive or Negative Number form 1 to 5
1
Enter the guess...
1
Wrong
Enter the guess...
a
Wrong
Enter the guess...
b
OK
['a', 'b', 'c', 'd', 'e']

```

I can't change what position in the list I want to guess on. Further, once I guess the position of 1 element, the game is over.

TLDR:
You need to do a better job of communicating to the user. Also, you should break your program into functions. Look into
```if __name__ == "__main__":

```

to see how your program can be improved.

#3 mikelcu

Reputation: 0
• Posts: 2
• Joined: 12-June 12

Re: Question: Guess program

Posted 17 June 2012 - 02:40 PM

Thanks a lot.

#4 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Question: Guess program

Posted 12 July 2012 - 07:08 PM

atraub, on 15 June 2012 - 09:16 PM, said:

```if __name__ == "__main__":

```

to see how your program can be improved.

I read up alittle on what you suggested to OP. Am I right in saying the following (in english terms, for both myself and everyone else):
if __name__ == "__main__":, essentially allows you to use a certain block of code in another script through the use of import? or no?

#5 atraub

• Pythoneer

Reputation: 760
• Posts: 2,015
• Joined: 23-December 08

Re: Question: Guess program

Posted 12 July 2012 - 07:32 PM

not exactly. When a module is ran directly through python, that module's name is __main__. When a module is imported into a different script, it's name is not main. Thus, the code will be ran only if that module is the starting point and it will not be ran if that module was imported into another script.

#6 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Question: Guess program

Posted 12 July 2012 - 07:43 PM

atraub, on 12 July 2012 - 09:32 PM, said:

not exactly. When a module is ran directly through python, that module's name is __main__. When a module is imported into a different script, it's name is not main. Thus, the code will be ran only if that module is the starting point and it will not be ran if that module was imported into another script.

meaning? (question mark becuase this is my interpretation and it is questionable) :
```if __name__ == '__main__':
```

The statements that are within the following block of code will only run if the module is directly in that script, but if it was say imported all the statements in the block of code will not run.?

#7 atraub

• Pythoneer

Reputation: 760
• Posts: 2,015
• Joined: 23-December 08

Re: Question: Guess program

Posted 12 July 2012 - 08:41 PM

more or less. play with it and you'll get a more clear idea of it.

#8 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

Re: Question: Guess program

Posted 12 July 2012 - 09:23 PM

atraub, on 12 July 2012 - 10:41 PM, said:

more or less. play with it and you'll get a more clear idea of it.

I plan on it =D, thanks for sharing your wisdom

#9 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

Re: Question: Guess program

Posted 14 July 2012 - 05:16 AM

Also, you shuffle the list every time the user inputs a new item. You only need to shuffle it once.