Click to See Complete Forum and Search --> : AAAAAAAHHHHHHH!!!!!


inkedmn
12-03-2001, 08:42 PM
i can't figure this out.

i'm getting an error on the line noted below, and i have no idea why...


import sys

n = 5

x = int(raw_input("Please enter a number between 1 and 10, or 11 to exit: "))
if x == n:
print "w00t! you win!"
while x != 5 and x != 11:
if x < 1 and x > 11:
print "You have entered an invalid number"
elif x == 11:
print "Quitting..."
sys.exit()
else: # THIS LINE
print "Sorry, that's incorrect, please try again"
x = int(raw_input("Please enter a number between 1 and 10, or 11 to exit: "))



any ideas?

[ 03 December 2001: Message edited by: inkedmn ]

George Kilroy
12-03-2001, 09:53 PM
I'm kinda new to python, but I think the problem is the while statement.

It think it should be:

a = 1
while a != 0:
x = raw_input("enter # between 1-10 >"
if x = 5:
print "correct\nquitting..."
a = 0 #this is where it ends the script
else:
print "incorrect"

...but wait till some experts varify this.

[ 03 December 2001: Message edited by: George Kilroy ]

[ 03 December 2001: Message edited by: George Kilroy ]

vee-eye
12-03-2001, 11:00 PM
It looks fine to me. :confused:

What is the error message?

[ 03 December 2001: Message edited by: vee-eye ]

scanez
12-03-2001, 11:13 PM
Hmmm, I basically know jack s*** about python but I don't think this is possible:

if x < 1 and x > 11:

shouldn't that be

if x < 1 or x > 11:

?

No idea if that will help the error or not but it may help prevent others :)
SC

[ 03 December 2001: Message edited by: scanez ]

joelmon
12-04-2001, 12:19 AM
Here's what I did to get it to work

The script you had would endlessly loop the text message, so I fixed that

And this also tells if 0 or 12 +++ (> 11)
is entered, not just if the number you enter doesn't = the entered number. Here you go (Note, I replaced your x variable with guess and n with number):

import sys
number = 9
guess = 0
over = 10
start = 1

while guess != number :
guess = input ("Guess a number between 1 and 10. Enter 11 to quit: ")

if guess == 11 :
print "Quitting"
sys.exit()

elif guess < 1 or guess > 11:
print "Sorry, that is not a valid number. Please choose between 1 and 10 only"

elif guess != number :
print "Keep trying"

print "wOOt! you win!"

joelmon
12-04-2001, 12:22 AM
Note: IF the indentation doesn't print well, you can email me and I can attach it for ya

Sorry, I think you would know the indentation
for both 'elif' and if are 4 spaces from far left and print below is one further to the right of the if/elif statements

just fyi. I see the response code of mine, though it works, shows no indentations

Enjoy, this works for me

inkedmn
12-04-2001, 01:49 AM
ok, here it is. it works, but i know there's a way i can do this without having the "x = ..." line in there 3 times.


import sys

number = 5

x = int(raw_input("Please enter a number between 1 and 10, or 11 to quit: "))
if x == 5:
print "w00t! you win!"
while x != 5:
if x == 11:
print "Quitting..."
sys.exit()
elif x < 1 or x > 11:
print "Invalid entry, please try again"
x = int(raw_input("Please enter a number between 1 and 10, or 11 to quit: "))
else:
print "sorry, wrong answer, please try again"
x = int(raw_input("Please enter a number between 1 and 10, or 11 to quit: "))


thanks :)

scanez
12-04-2001, 01:54 AM
How about this:


import sys

number = 5

while true:
x = int(raw_input("Please enter a number between 1 and 10, or 11 to quit: "))
if x == 5:
print "w00t! you win!"
sys.exit()
elif x == 11:
print "Quitting..."
sys.exit()
elif x < 1 or x > 11:
print "Invalid entry, please try again"
else:
print "sorry, wrong answer, please try again"


Would that work?
SC

joelmon
12-04-2001, 03:59 AM
Try what I suggested earlier. It works

same concept

I tested it

TheLinuxDuck
12-04-2001, 10:25 AM
#!/usr/bin/python
from random import Random, random
 
Random()
 
quit = 0
tries = 0
max = int((random()*75))+25 # 25-99
min = 1
number = int((random()*(max-(min-1)))+1)
 
print "What is the magic number, between", min, "and", max, " (-1 to quit)?"
while quit == 0:
try:
guess = int(raw_input(">"))
if guess == -1:
print "Quitting..."
quit = 1
elif guess < min or guess > max:
print "Invalid number, ", guess, " Must be between", min, "and", max
elif guess < number:
tries += 1
print "Nope. Higher, jackass"
elif guess > number:
tries += 1
print "Nope. Lower, jackass"
else:
print "You got it right in", tries, "tries, jackass!"
quit = 1
except ValueError:
print "That was not a number"
except KeyboardInterrupt:
print "You'll have to type -1, or guess the number to quit"


TLD's version. (^=

Aragorn
12-05-2001, 02:22 AM
Originally posted by inkedmn:
<STRONG>i can't figure this out.

i'm getting an error on the line noted below, and i have no idea why...


import sys

n = 5

x = int(raw_input("Please enter a number between 1 and 10, or 11 to exit: "))
if x == n:
print "w00t! you win!"
while x != 5 and x != 11:
if x &lt; 1 and x &gt; 11:
print "You have entered an invalid number"
elif x == 11:
print "Quitting..."
sys.exit()
else: # THIS LINE
print "Sorry, that's incorrect, please try again"
x = int(raw_input("Please enter a number between 1 and 10, or 11 to exit: "))



any ideas?

[ 03 December 2001: Message edited by: inkedmn ]</STRONG>

Ummmm your code should work fine except for your if statement....it is erroring on else because your if statement is impossible, if you change the and to or it should work like a charm....

Aragorn

inkedmn
12-05-2001, 12:16 PM
ok, this is the revised (and working) version.

after putting the request for user input inside the while loop, i was able to take out the other two instances of that line, and by using 'break', i was also able to avoid having to import the sys module...


#!/usr/bin/env python

number = 5

while 1:
x = int(raw_input("Please enter a number between 1 and 10, or 11 to quit: "))
if x == 11:
print "Quitting..."
break
elif x &lt; 1 or x &gt; 11:
print "Invalid Selection, try again"
elif x == number:
print "w00t! you win!"
break
else:
print "Incorrect, please try again"


w00t!

Gnu/Vince
12-05-2001, 04:24 PM
#!/usr/bin/env ruby

while true
num = rand(10)

print "Enter number (1..10): "
if not guess = /^\s*(\d*)\s*$/.match(gets) then
puts "This is not a number"
next
end

guess = guess[1].to_i

if guess == 11 then
puts "Goodbye!"
break
end

if ((guess &lt; 0) or (guess &gt; 11)) then
puts "This is not a valid number"
next
end

if guess[1] == num then
puts "Yeah! This is the right number!!"
else
puts "Too bad... better luck next time!"
end
end


[ 05 December 2001: Message edited by: Gnu/Vince ]