473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Having trouble coding a beginner game

15
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
13 2665
dazzler
75
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
Laharl
849 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
Hamy Li
15
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
2,851 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
Hamy Li
15
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
Laharl
849 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
Hamy Li
15
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
Laharl
849 Expert 512MB
Did your book cover reverse/negative indexing? That might be of use...
Mar 14 '08 #9
jlm699
314 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
2,851 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
Hamy Li
15
thanks alot to all of you.
great help.
Mar 15 '08 #12
Hamy Li
15
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
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

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

Similar topics

2
by: Lee Garrington | last post by:
Hey, Recently I decided to learn C++ so that I could port over one of my Java programs to make it faster. Basically everything has ported over fine so far until I came up against the following...
14
by: BlackHawke | last post by:
My Name is Nick Soutter, I am the owner of a small game company, Aepox Games (We're in the middle of a name change from "Lamar Games"), www.lamargames.net. Our first commercial game,...
4
by: TempMan | last post by:
I want this text field to always display a number variable. The variable "num" is defined in the head, how can I get a text box to display this varibale?? <input name="Balance" type="text"...
2
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2)....
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
6
by: JNeko | last post by:
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
4
by: softwaregeek | last post by:
hi to all, I am having some problem in passing parameter..... My code looks like::: request.setAttribute("game",game);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.