#----------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
Question: Guess program
Page 1 of 18 Replies - 11819 Views - Last Post: 14 July 2012 - 05:16 AM
#1
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
Replies To: Question: Guess program
#2
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:
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
to see how your program can be improved.
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.
#4
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
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
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
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
Re: Question: Guess program
Posted 12 July 2012 - 09:23 PM
#9
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.
Page 1 of 1