8 Replies - 6816 Views - Last Post: 14 July 2012 - 05:16 AM Rate Topic: -----

#1 mikelcu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 mikelcu  Icon User is offline

  • New D.I.C Head

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

Re: Question: Guess program

Posted 17 June 2012 - 02:40 PM

Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#4 k3y  Icon User is offline

  • D.I.C Head

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

Re: Question: Guess program

Posted 12 July 2012 - 07:08 PM

View Postatraub, 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?
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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.
Was This Post Helpful? 2
  • +
  • -

#6 k3y  Icon User is offline

  • D.I.C Head

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

Re: Question: Guess program

Posted 12 July 2012 - 07:43 PM

View Postatraub, 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.?
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 k3y  Icon User is offline

  • D.I.C Head

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

Re: Question: Guess program

Posted 12 July 2012 - 09:23 PM

View Postatraub, 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
Was This Post Helpful? 0
  • +
  • -

#9 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1