467,903 Members | 1,875 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,903 developers. It's quick & easy.

Having trouble coding a beginner game

In a book that I'm learning, there is a project which I have to create a game called "Guess my number". In this game, I pick a number from 1 to 100 and let the computer guess my number. I don't know how code control the numbers i type

Expand|Select|Wrap|Line Numbers
  1. .
  2.  
  3. number = raw_input("Enter a Number: ")
  4. if number < "1":
  5.   print "invalid"
  6. if number > "100":
  7.   print "invalid
  8. else:
  9.   print "continue"
  10.  
  11.  
i don't know what is wrong with this
can anyone please help. will really appreciate
I'm using python 2.5
Mar 7 '08 #1
  • viewed: 2434
Share:
13 Replies
Expand|Select|Wrap|Line Numbers
  1. .
  2.  
  3. number = raw_input("Enter a Number: ")
  4. if number < "1":
  5.   print "invalid"
  6. if number > "100":
  7.   print "invalid
  8. else:
  9.   print "continue"
  10.  
  11.  
instead of if number < "1":
try this if number < 1:
if you use "" it means that it is text not numbers

and now your raw_input will be saved as text (string) also, and it can't compare text and numbers, so you have to convert it to numeric using float() or int()

number = float(raw_input("enter number"))
Mar 7 '08 #2
Expert 512MB
You need to convert your raw_input to an integer with the int() function. Thus, you can use integer comparisons (which will give the correct results), not string comparisons.

Expand|Select|Wrap|Line Numbers
  1. >>>number = raw_input("Please enter a number: ")
  2. Please enter a number: 14
  3. >>>print number
  4. 14
  5. >>>number < "100"
  6. False
  7. >>>number > "1"
  8. True
  9. >>>number = int(raw_input("Please enter a number: ")
  10. Please enter a number: 14
  11. >>>number < 100
  12. True
  13.  
The >>> means this is code that I ran on the Python interactive shell.
Mar 7 '08 #3
thank you very much for all your help. really saved my time.
I also have a new question based on the same game.

Expand|Select|Wrap|Line Numbers
  1. import random
  2. print "\tWelcome to 'Guess My Number'!"
  3. print "\nI'm thinking of a number between 1 and 100."
  4. print "Try to guess it in as few attempts as possible.\n"
  5. # set the initial values
  6. the_number = random.randrange(100) + 1
  7. guess = int(raw_input("Take a guess: "))
  8. tries = 1
  9. # guessing loop
  10. while (guess != the_number):
  11.     if (guess > the_number):
  12.         print "Lower..."
  13.     else:
  14.         print "Higher..."
  15.  
  16.     guess = int(raw_input("Take a guess: "))
  17.     tries += 1
  18. print "You guessed it! The number was", the_number
  19. print "And it only took you", tries, "tries!\n"
  20.  
how do i reverse this game so that i pick a number and the computer has to guess what number it is.
I tried to code it but i got stuck.
any help would be grateful
Mar 7 '08 #4
bvdet
Expert Mod 2GB
Here I will use random.choice for the computer to select a number. I will give the computer a hint 'lower' and 'higher' each time. Each choice will be removed from the possible choices.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def guess_number_rev(lower=1, upper=100):
  4.     number = int(raw_input('Enter a number between %d and %d' % (lower, upper)))
  5.     # list of possible guesses
  6.     guessList = (range(lower,upper+1))
  7.     if number not in guessList:
  8.         print 'Invalid entry! Exiting...'
  9.         return
  10.     # get a random number
  11.     guess = random.choice(range(lower,upper+1))
  12.     # remove first guess
  13.     guessList.remove(guess)
  14.     # create a list of guesses the computer made
  15.     guesses = [guess, ]
  16.     while True:
  17.         if guess > number:
  18.             guess = random.choice([num for num in guessList if num < guess])
  19.         elif guess < number:
  20.             guess = random.choice([num for num in guessList if num > guess])
  21.         else:
  22.             break
  23.         guessList.remove(guess)
  24.         guesses.append(guess)
  25.     print 'The number entered: %d' % number
  26.     print 'It took the computer %d guesses' % (len(guesses))
  27.     print 'Guesses made:\n%s' % '\n'.join([str(n) for n in guesses])
  28.  
  29. guess_number_rev()  
  30.  
Sample output:
>>> The number entered: 67
It took the computer 8 guesses
Guesses made:
49
79
23
29
61
94
62
67
>>>
Mar 8 '08 #5
thanks for the help. may I ask what version of python you are using?
because when i wrote the code in python and tried to run it
if comes up with this error

Token Error: EOF in multi-line statement

what does this error mean?
Mar 8 '08 #6
Expert 512MB
Check your indentation/spacing at the very end of the file, as you've probably got something screwed up there.
Mar 8 '08 #7
A new question.
In a book I'm using, It has a few challenges every chapter. In the chapter I'm reviewing right now, It wants me make a program where the computer lets the user write a message the then prints the message backwards. I know this may sound very easy, but all i can use in my coding is the skills in that chapter.
the skills included in that chapter is
indexing
slicing
for loops
random.choice
creating tuples
using tuples.

using only these skills, i can't seem to figure how to print a message backwards.

Expand|Select|Wrap|Line Numbers
  1. print "Welcome to the backward machine. "
  2. print "Write in a message and the computer will print it backwards"
  3.  
  4. message = raw_input("Enter a message: ")
there were my first 3 lines. after that, I tried using indexing and for loops but non came out right.
Please help.

thank you
Mar 14 '08 #8
Expert 512MB
Did your book cover reverse/negative indexing? That might be of use...
Mar 14 '08 #9
jlm699
100+
Are you allowed to use list reversal?

Expand|Select|Wrap|Line Numbers
  1. print 'Welcome to the backward machine.'
  2. print 'Write in a message and the computer will print it backwards'
  3.  
  4. message = raw_input('Enter a message: ')
  5. new_msg = ''
  6. rev_idx = range(len(message))
  7. rev_idx.reverse()
  8. for idx in rev_idx:
  9.     new_msg += message[idx]
  10.  
  11. print 'The backwards message is:', new_msg
Output:
Expand|Select|Wrap|Line Numbers
  1. Microsoft Windows XP [Version 5.1.2600]
  2. (C) Copyright 1985-2001 Microsoft Corp.
  3.  
  4. C:\Documents and Settings\Administrator\Desktop\pythtests>python bckmch.py
  5. Welcome to the backward machine.
  6. Write in a message and the computer will print it backwards
  7. Enter a message: wtf
  8. The backwards message is: ftw
  9.  
  10. C:\Documents and Settings\Administrator\Desktop\pythtests>
  11.  
Mar 14 '08 #10
bvdet
Expert Mod 2GB
Here is another possibility:
Expand|Select|Wrap|Line Numbers
  1. >>> word = 'supposition'
  2. >>> print ''.join([word[i] for i in range(len(word)-1, -1, -1)])
  3. noitisoppus
  4. >>> 
Mar 14 '08 #11
thanks alot to all of you.
great help.
Mar 15 '08 #12
the below program chooses a random word then asks the user to guess a letter.
the user has 5 tries to guess a letter. After 5 tries, the user has to guess the word its self. I tried coding this program. It worked, but i want to know if there is a better way of writing this program. because as you can see, it is very long for such a small program. how can a loop be used in this program instead of the separate 5 if-else commands.

Expand|Select|Wrap|Line Numbers
  1. import random
  2. WORDS = ("happy", "sad", "congratulations", "normal", "bad", "smart")
  3. word = random.choice(WORDS)
  4. tries = 1
  5. print "There are", len(word), "letters in this word."
  6.  
  7. guess = raw_input("Enter the letter you want to guess: ")
  8. if guess in word:
  9.     print "\nthe letter you picked is in the word. "
  10. else:
  11.     print "\nthe letter is not in the word"
  12. tries += 1
  13.  
  14. guess = raw_input("Enter the letter you want to guess: ")
  15. if guess in word:
  16.     print "\nthe letter you picked is in the word. "
  17. else:
  18.     print "\nthe letter is not in the word"
  19. tries += 1
  20.  
  21. guess = raw_input("Enter the letter you want to guess: ")
  22. if guess in word:
  23.     print "\nthe letter you picked is in the word. "
  24. else:
  25.     print "\nthe letter is not in the word"
  26. tries += 1
  27.  
  28. guess = raw_input("Enter the letter you want to guess: ")
  29. if guess in word:
  30.     print "\nthe letter you picked is in the word. "
  31. else:
  32.     print "\nthe letter is not in the word"
  33. tries += 1
  34.  
  35. guess = raw_input("Enter the letter you want to guess: ")
  36. if guess in word:
  37.     print "\nthe letter you picked is in the word. "
  38. else:
  39.     print "\nthe letter is not in the word"
  40. tries += 1
  41.  
  42. print " you do not have any more guesses"
  43. print "please guesses the word"
  44. guess_words = raw_input("\nEnter you guess of the word: ")
  45. if guess_words == word:
  46.     print "Congratulations, you guessed it"
  47. else:
  48.     print "sorry, you failed"
  49.  
  50. raw_input("\n\nPress the Enter key to exit")

thanks

ohh. and how do i post codes as python codes so the color shows?
Mar 15 '08 #13
Subsciber123
Firstly, you show python code by using the code tags, but putting "=python" after the word "CODE"

Secondly, yes, you can use loops.

The first thing you should do is get rid of the variable "tries". You don't use it for anything.
Next, put one of your if statements inside a for loop as so, and get rid of the rest of them:
Expand|Select|Wrap|Line Numbers
  1. for i in range(5):
  2.     guess = raw_input("Enter the letter you want to guess: ")
  3.     if guess in word:
  4.         print "\nthe letter you picked is in the word."
  5.     else:
  6.         print "\nthe letter is not in the word"
Mar 15 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Lee Garrington | last post: by
14 posts views Thread by BlackHawke | last post: by
4 posts views Thread by TempMan | last post: by
2 posts views Thread by ed | last post: by
1 post views Thread by yucikala | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.