0

Hi, this is my first post. Ive made a number game which is written in python 2.5 and have some problems with it:

-------------------------------------

import random

# You've got to guess 4 numbers between 0 and 30, if you one right the program will output "WINNER" and tell you the computer's numbers

x = random.randint(0,30)
y = random.randint(0,30)
z = random.randint(0,30)
t = random.randint(0,30)


print "Enter 4 numbers between 0 and 30"
if raw_input() == x:
print "WINNER"
elif raw_input() != x:
print "LOSER"

print "The Computer's Numbers Were:"
print x,y,z,"and",t

-----------------------------------------------

I basically want to be able to enter 4 numbers, each separated by a space, and have the computer compare the inputted numbers to its numbers, if a number is right, the program will print WINNER or LOSER and then show the computers numbers. Im halfway there I just need a little extra help

Thanks,

Laurence:idea:

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by LaurenceB
0

I'll be honest with you I've never written in Python, but you could do something like:

If raw_input() == x or raw_input() == y or raw_input() == z or raw_input() == t:
print "WINNER"
else
print "Loser"

print "The Computer's Numbers Were:"
print x,y,z,"and",t

I don't know enough about python to know how to parse the 4 numbers from a raw input, but I would almost recommend you request the "user" to enter one number at a time, then just simply store off the raw input as different variables each.

I hope that helps

onaclov2000

Hi, this is my first post. Ive made a number game which is
written in python 2.5 and have some problems with it:

-------------------------------------

import random

# You've got to guess 4 numbers between 0 and 30, if you one right the program will output "WINNER" and tell you the computer's numbers

x = random.randint(0,30)
y = random.randint(0,30)
z = random.randint(0,30)
t = random.randint(0,30)


print "Enter 4 numbers between 0 and 30"
if raw_input() == x:
print "WINNER"
elif raw_input() != x:
print "LOSER"

print "The Computer's Numbers Were:"
print x,y,z,"and",t

-----------------------------------------------

I basically want to be able to enter 4 numbers, each separated by a space, and have the computer compare the inputted numbers to its numbers, if a number is right, the program will print WINNER or LOSER and then show the computers numbers. Im halfway there I just need a little extra help

Thanks,

Laurence:idea:

0

Just to give a rough idea ...

import random

x = random.randint(0,30)
y = random.randint(0,30)
z = random.randint(0,30)
t = random.randint(0,30)

list1 = [x, y, z, t]
print "Enter 4 numbers between 0 and 30:"
for k in range(1, 5):
    prompt = str(k) + " --> "
    choice = int(raw_input(prompt))
    if choice in list1:
        print "WINNER"
        break
# this else goes with the for loop
else:
    print "LOSER"

print "The Computer's Numbers Were:"
print x, y, z, "and", t
0

And if you are using a list, then the individual variable for each random number isn't necessary.

computer_list = []
for j in range(0, 4):
   computer_list.append(random.randint(0, 30))
print "The computer's numbers are", computer_list
#
#
#    To enter all 4 numbers on one line, use the split method
numbers = raw_input("Enter 4 numbers between 0-30 separated by a space ")
numbers_list = numbers.split()
if len(numbers_list) != 4:
   print "4 numbers were not entered"
else:
   lit = "Loser"
   for number in numbers_list:
      if int(number) in computer_list:
         lit = "You found number " + number
   print lit
0

Thank you so much, all of your replies have helped so much, now the program actually works properly. This community is fantastic!

Thanks,

Laurence

0

The program works well but there is one small flaw: when you enter a winning number, it prints winner and shows you the computers numbers instead of letting you enter all of your numbers:

Screenshot

I want the program to wait until all the numbers have been entered before it tells you wheather you are a winner or a loser:

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.