简体   繁体   中英

Python input numbers and select the winning numbers from a predefined list

I just started learning Python yesterday, it's my first contact with a programming language and I've noticed that I learn better by doing something than by only reading so I'm giving myself different tasks and bash my head until I manage to complete them. Regarding my problem, I'm sure that there's a chance that this question has been asked before, but I can't seem to find it...sorry. So, I want to make a little program that does the following: I have a predefined set of winning lottery numbers. I want the user to input 6 numbers. If all six are winners, the user receives a message like: "You Won!", if only 2 numbers are correct, the message is: "You guessed two numbers", if 3 numbers are correct...etc, you get the point.

Here is what i have so far:

winning_numbers = ("6", "8", "12", "21", "33", "47")
input_numbers = input("Input your lottery numbers ")

for i in winning_numbers: #I've added i in here because I wanted tot try a loop, but i am stuck...
    if input_numbers == winning_numbers:
        print("You guessed all six numbers!")
    elif input_numbers in (winning_numbers[0], winning_numbers[1], winning_numbers[2], winning_numbers[3], winning_numbers[4], winning_numbers[5]): #I am sure that this can be shorter but I don't know how
        print("You guessed one number!")
    elif input_numbers in winning_numbers #I'm stuck here also, where 2 numbers from the input are correct:
        print("You guessed two numbers!")
    else:
        print("You are a looser!")

I know it can be done MUCH more simpler than this but I have no idea how. Maybe if someone gives me a hint, I'll be able to understand the concept behind this. Thank you in advance.

You may want to try a more "complex" approach:

winning_numbers = ("6", "8", "12", "21", "33", "47")
input_numbers = input("Input your lottery numbers ").split(",")

correct_guesses = sum(1 for inp_num in input_numbers if inp_num in winning_numbers)

print("You guessed {} numbers".format(correct_guesses))

How does it work?

  1. You need to call .split on input_numbers because user will presumably input comma-separated numbers, but it will be one long string(for example "1,2,3" ). split(",") will turn this comma-separated string to a list (for example ["1", "2", "3"] ).

  2. 1 for inp_num in input_numbers if inp_num in winning_numbers returns a generator containing the integer 1 for every number in input_numbers that is also in winning_numbers .

  3. sum will simply sum these 1 s.

winning_numbers = ["6", "8", "12", "21", "33", "47"]
c = 0
for i in xrange(len(winning_numbers)):
    n = raw_input('enter your number')
    if n in winning_numbers:
        c+=1
print 'You guessed %s numbers correctly!'%c

this is python 2.7

The %s means I'm inserting an external string in to the string. % is called string formatting and you can add external variables in to a string by using the percentage sign. %s just means i'm inserting a string, for instance: 'hello %s'%'world!' will be the string 'hello world!' .

For more information about string formatting look at the documentation .

Try counting the correct numbers using a variable.

winning_numbers = ('6', '8', '12', '21', '33', '47')
input_numbers = input('Input your lottery numbers ')

correct = 0
for i in winning_numbers:
    if i in input_numbers:
        correct += 1

if correct == 6:
    print('You guessed all six numbers!')
elif correct >= 2:
    print('You guessed at least two numbers!')
elif correct == 1:
    print('You guessed one number!')
else:
    print('You are a looser!')

You can also use % to simplify your printing

if correct > 0:
    print('You guessed %d number(s)!' % (correct))
else:
    print('You are a looser!')

The % will replace the corresponding part of the string with the value in the following brackets. See this link for more detail.

Or if you are a one-liner

winning_numbers = ('6', '8', '12', '21', '33', '47')
correct = len([x for x in input('Input your lottery numbers ').split() if x in winning_numbers])
print('You guessed %d number(s)!' % (correct))

[x for x in input('Input your lottery numbers ').split()] will create a list containing the user input. The following if x in winning_numbers will make sure that the list will only contain the elements which are in the winning_numbers . If you remove the if part, then the string will contain all the elements in the user input. Then we use len() to calculate the length of the list, that is the number of correct guess.

The above trick is called "list comprehension". Check this link for more detail.

I only started learning Python 2 weeks ago so I am new as well. Good luck with your learning. Here is my solution!

correct_numbers = ("1", "2", "3", "4", "5", "6")
input_numbers = input("Input your 6 numbers")
count = 0

for i in correct_numbers:
    if i in input_numbers.split():
    count += 1

if count == 6:
   print("You guessed all numbers")
else:
   print("You guessed {} numbers correctly".format(count))

The two curly brackets in my last piece of code is one of the many ways to input a variable into a string (my personal favourite way to do it). The way it works is that the two curly brackets show where the variable has to be inserted, and once you finish the string you put to call in the function and put the variables you want to input in a set of brackets after the .format... Make sure to put the variables in order

One example:

var1 = two
print("one {}".format(var1))
# the output of this will be "one two"

Second example:

var1 = two
var2 = four
print("one {} three {}".format(var1, var2))
# the output of this would be "one two three four"

HOPE THIS HELPS :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM