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 - .
-
-
number = raw_input("Enter a Number: ")
-
if number < "1":
-
print "invalid"
-
if number > "100":
-
print "invalid
-
else:
-
print "continue"
-
-
i don't know what is wrong with this
can anyone please help. will really appreciate
I'm using python 2.5
13 2665 - .
-
-
number = raw_input("Enter a Number: ")
-
if number < "1":
-
print "invalid"
-
if number > "100":
-
print "invalid
-
else:
-
print "continue"
-
-
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"))
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. -
>>>number = raw_input("Please enter a number: ")
-
Please enter a number: 14
-
>>>print number
-
14
-
>>>number < "100"
-
False
-
>>>number > "1"
-
True
-
>>>number = int(raw_input("Please enter a number: ")
-
Please enter a number: 14
-
>>>number < 100
-
True
-
The >>> means this is code that I ran on the Python interactive shell.
thank you very much for all your help. really saved my time.
I also have a new question based on the same game. - import random
-
print "\tWelcome to 'Guess My Number'!"
-
print "\nI'm thinking of a number between 1 and 100."
-
print "Try to guess it in as few attempts as possible.\n"
-
# set the initial values
-
the_number = random.randrange(100) + 1
-
guess = int(raw_input("Take a guess: "))
-
tries = 1
-
# guessing loop
-
while (guess != the_number):
-
if (guess > the_number):
-
print "Lower..."
-
else:
-
print "Higher..."
-
-
guess = int(raw_input("Take a guess: "))
-
tries += 1
-
print "You guessed it! The number was", the_number
-
print "And it only took you", tries, "tries!\n"
-
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
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. - import random
-
-
def guess_number_rev(lower=1, upper=100):
-
number = int(raw_input('Enter a number between %d and %d' % (lower, upper)))
-
# list of possible guesses
-
guessList = (range(lower,upper+1))
-
if number not in guessList:
-
print 'Invalid entry! Exiting...'
-
return
-
# get a random number
-
guess = random.choice(range(lower,upper+1))
-
# remove first guess
-
guessList.remove(guess)
-
# create a list of guesses the computer made
-
guesses = [guess, ]
-
while True:
-
if guess > number:
-
guess = random.choice([num for num in guessList if num < guess])
-
elif guess < number:
-
guess = random.choice([num for num in guessList if num > guess])
-
else:
-
break
-
guessList.remove(guess)
-
guesses.append(guess)
-
print 'The number entered: %d' % number
-
print 'It took the computer %d guesses' % (len(guesses))
-
print 'Guesses made:\n%s' % '\n'.join([str(n) for n in guesses])
-
-
guess_number_rev()
-
Sample output:
>>> The number entered: 67
It took the computer 8 guesses
Guesses made:
49
79
23
29
61
94
62
67
>>>
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?
Check your indentation/spacing at the very end of the file, as you've probably got something screwed up there.
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. -
print "Welcome to the backward machine. "
-
print "Write in a message and the computer will print it backwards"
-
-
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
Did your book cover reverse/negative indexing? That might be of use...
Are you allowed to use list reversal? - print 'Welcome to the backward machine.'
-
print 'Write in a message and the computer will print it backwards'
-
-
message = raw_input('Enter a message: ')
-
new_msg = ''
-
rev_idx = range(len(message))
-
rev_idx.reverse()
-
for idx in rev_idx:
-
new_msg += message[idx]
-
-
print 'The backwards message is:', new_msg
Output: -
Microsoft Windows XP [Version 5.1.2600]
-
(C) Copyright 1985-2001 Microsoft Corp.
-
-
C:\Documents and Settings\Administrator\Desktop\pythtests>python bckmch.py
-
Welcome to the backward machine.
-
Write in a message and the computer will print it backwards
-
Enter a message: wtf
-
The backwards message is: ftw
-
-
C:\Documents and Settings\Administrator\Desktop\pythtests>
-
bvdet 2,851
Expert Mod 2GB
Here is another possibility: - >>> word = 'supposition'
-
>>> print ''.join([word[i] for i in range(len(word)-1, -1, -1)])
-
noitisoppus
-
>>>
thanks alot to all of you.
great help.
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. - import random
-
WORDS = ("happy", "sad", "congratulations", "normal", "bad", "smart")
-
word = random.choice(WORDS)
-
tries = 1
-
print "There are", len(word), "letters in this word."
-
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word. "
-
else:
-
print "\nthe letter is not in the word"
-
tries += 1
-
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word. "
-
else:
-
print "\nthe letter is not in the word"
-
tries += 1
-
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word. "
-
else:
-
print "\nthe letter is not in the word"
-
tries += 1
-
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word. "
-
else:
-
print "\nthe letter is not in the word"
-
tries += 1
-
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word. "
-
else:
-
print "\nthe letter is not in the word"
-
tries += 1
-
-
print " you do not have any more guesses"
-
print "please guesses the word"
-
guess_words = raw_input("\nEnter you guess of the word: ")
-
if guess_words == word:
-
print "Congratulations, you guessed it"
-
else:
-
print "sorry, you failed"
-
-
raw_input("\n\nPress the Enter key to exit")
thanks
ohh. and how do i post codes as python codes so the color shows?
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: - for i in range(5):
-
guess = raw_input("Enter the letter you want to guess: ")
-
if guess in word:
-
print "\nthe letter you picked is in the word."
-
else:
-
print "\nthe letter is not in the word"
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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"...
|
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)....
|
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...
|
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...
|
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...
|
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() ! */
...
|
by: softwaregeek |
last post by:
hi to all,
I am having some problem in passing parameter.....
My code looks like:::
request.setAttribute("game",game);...
|
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,...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |