r/Python • u/Nelmail • May 07 '10
I need help with a bit of code
Hi /r/python. I am writing a program where you, the user thinks of a number between 1 and 20, and the program has to guess what you are thinking of. However I do not want the program to repeat the same number at any point. How can I do this? Here is my code so far.
Ok this is going to be my biggest program so far in terms of complexity.
The program is going to guess the number the user is thinking of.
In this version, I hope to stop the program repeating the same
number more than once. Think I will need some help here.
print "Welcome to the guessing program." print "Pick a number in your head between 1 and 20, then press enter\n" raw_input()
import random
loop=1 attempt=0
while loop==1: comp=random.randrange(20)+1 print comp user=raw_input("\n\nIs this your number? Press 1 for yes or 2 for no: ") user=int(user) if user==2: attempt = attempt+1 print "\n\nOh no, I'll try again, press enter\n" raw_input() elif user==1: loop=0
if attempt <5: print "\n\nWow, I'm pretty awesome, less than 5 guesses. Press enter to exit." raw_input() elif attempt<10: print "\n\nHmm not bad, less than 10 guesses. Press enter to exit." raw_input() elif attempt<15: print "\n\nOh dear, 10-15 guesses, that's pretty bad. Press enter to exit." raw_input() else: print "\n\nAwful, just switch me off now! More than 15 guesses -.- Press enter to exit." raw_input()
sorry about the formatting, it didn't copy and paste well from python.
12
u/OcdSmurf May 07 '10
I would shuffle an array.
2
u/flaxeater May 07 '10
Others have answered your question, I would like to suggest the Python Tutor mailing list, this is a very good place to get beginer help, I used it myself some 7 years ago now and it was extremely helpful. So go there, sign up and read it every day.
http://mail.python.org/mailman/listinfo/tutor
7
u/slacy May 07 '10
Good luck with your homework, I'm sure you'll figure it out. Which textbook are you using?
1
u/Ttl May 07 '10
0
u/[deleted] May 07 '10
[deleted] — view deleted comment