在Python随机数生成器

时间:2015-11-02 04:36:38

标签: python python-3.x

我对Python相当新,我正在做一个"彩票游戏。"

我应该注意我使用的是Python 3.4.2Python GUI

该计划是一个彩票游戏,其中:

  • 生成两个随机数,每个数字介于0和9之间
  • 允许用户猜测两个数字(0到9之间)
  • 使用适当的消息提示用户输入输入
  • 将每个用户的猜测与两个随机数进行比较,并显示一条消息,其中包含用户猜测的2位数字

对于游戏,根据您的猜测,您可以根据以下条件获胜:

  • 任何一个匹配----> $ 1000
  • 两个匹配,不按顺序----> $ 10,000个
  • 两个匹配的确切顺序----> $ 100,000
  • 没有匹配----> $ 0
例如,我会猜测4 5并且生成的随机数将是6 4但是我会得到0美元或者#34;没有匹配。"

我认为错误在于无法获得"任何一个匹配"正确的,或这段代码:

elif guess1 == number1 | guess1 == number2 | guess2 == number1 | guess2 == number2:
    number1 = str(number1)
    number2 = str(number2)
    guess1 = str(guess1)
    guess2 = str(guess2)
    print('Your guess was ' + guess1 + guess2)
    print('The lottery number is ' + number1 + number2)
    print('Congrats, you win 1,000')

这是我写的代码:

import random

number1 = random.randint(0, 9)
number2 = random.randint(0, 9)

print('Welcome to the Lottery Game! You must guess two numbers.')

print('Guess a number from 0 - 9')
guess1 = input()
guess1 = int(guess1)

print('Guess a number from 0 - 9')
guess2 = input()
guess2 = int(guess2)

if guess1 == number1 & guess2 == number2:
    number1 = str(number1)
    number2 = str(number2)
    guess1 = str(guess1)
    guess2 = str(guess2)
    print('Your guess was ' + guess1 + guess2)
    print('The lottery number is ' + number1 + number2)
    print('Congrats, you win 100,000')
elif guess1 == number2 & guess2 == number1:
    number1 = str(number1)
    number2 = str(number2)
    guess1 = str(guess1)
    guess2 = str(guess2)
    print('Your guess was ' + guess1 + guess2)
    print('The lottery number is ' + number1 + number2)
    print('Congrats, you win 10,000')
elif guess1 == number1 | guess1 == number2 | guess2 == number1 | guess2 == number2:
    number1 = str(number1)
    number2 = str(number2)
    guess1 = str(guess1)
    guess2 = str(guess2)
    print('Your guess was ' + guess1 + guess2)
    print('The lottery number is ' + number1 + number2)
    print('Congrats, you win 1,000')
else:
    number1 = str(number1)
    number2 = str(number2)
    guess1 = str(guess1)
    guess2 = str(guess2)
    print('Your guess was ' + guess1 + guess2)
    print('The lottery number is ' + number1 + number2)
    print('Sorry, you win nothing.')

非常感谢任何帮助!

0 个答案:

没有答案