比较Python中的彩票程序的两位数字

时间:2013-03-03 03:12:33

标签: python-3.x

我在我的python类中有一个我正在努力的任务。

程序

这是基本前提: 彩票计划。程序随机生成一个两位数的数字,提示用户输入一个两位数的数字,并根据以下规则确定用户是否获胜:

  1. 如果用户的输入符合完全顺序中的抽奖,奖励为$ 10,000
  2. 如果用户输入中的所有数字都与彩票号码中的所有数字相符,则奖励为$ 1,000
  3. 如果用户输入中的一位数字与彩票号码中的数字相匹配,奖励为$ 1,000
  4. 基本格式

    基本上,我使用randint生成一个两位数字(比如说它生成58) 然后用户输入一个相同长度的数字(虽然没有指定,但为了简单起见,我们可以说数字是10到99)

    然后通过一系列嵌套ifs将数字与3个结果和1个异常进行比较。

    问题:

    我不知道如何以指定的方式比较数字。我知道所有的基本操作符,但在这种情况下我没有看到使用它们的方法(除了完全匹配的数字,你可以使用==)。我在考虑一个数组(来自我的C / C ++类),但我不确定如何在这里实现它。这是我到目前为止所做的:

    import random
    import time
    
    ##Declare Variables
    usernum=0.0
    lottery_num=random.randint(10,99)
    ##Input
    print("Welcome to the Lottery Program!")
    usernum=int(input("Please enter a two digit number: "))
    print("Calculating Results.")
    for i in range(3):
      time.sleep(1)
      print(".")
    ##Calc & Output
    if lottery_num==usernum:
      print("All your numbers match in exact order! Your reward is $10,000!\n")
    elif lottery_num==                #WHAT DO HERE?
      print("All your numbers match! Your reward is $3,000!\n")
    elif lottery_num==                #WHAT DO HERE?
      print("One of your numbers match the lottery. Your reward is $1,000!\n")
    else:
     print("Your numbers don't match! Sorry!")
    

    解决方案

    我终于想出了如何在你们的帮助下做到这一点!非常感谢你!对于那些对我所做的事感兴趣的人来说,这是完整的作业。

    import random
    import time
    
    ##Declare Variables
    user_num=0
    ##lottery_num=random.randint(10,99)
    lottery_num=12
    
    ##Input
    print("Welcome to the Lottery Program!")
    user_num=int(input("Please enter a two digit number: "))
    print("Calculating Results.")
    for i in range(3):
      time.sleep(1)
      print(".")
    
    ##Calc & Output
    lottery_tens = lottery_num // 10
    lottery_ones = lottery_num % 10
    
    user_tens = user_num // 10
    user_ones = user_num % 10
    
    if lottery_num == user_num:
        print("All your numbers match in exact order! Your reward is $10,000!\n")
    elif lottery_tens == user_ones and lottery_ones == user_tens:
        print("All your numbers match! Your reward is $3,000!\n")
    elif lottery_tens == user_tens or lottery_ones == user_ones \
      or lottery_ones == user_tens or lottery_tens == user_ones:
        print("One of your numbers match the lottery. Your reward is $1,000!\n")
    else:
        print("Your numbers don't match! Sorry!")
    
    ##Same as Calc & Output using Sets.
    ##lottery_set = set('%02d' % lottery_num)
    ##user_set = set('%02d' % user_num)
    ##if lottery_num == user_num:
    ##    print("All your numbers match in exact order! Your reward is $10,000!\n")
    ##elif lottery_set == user_set:
    ##    print("All your numbers match! Your reward is $3,000!\n")
    ##elif lottery_set.intersection(user_set):
    ##    print("One of your numbers match the lottery. Your reward is $1,000!\n")
    ##else:
    ##    print("Your numbers don't match! Sorry!")
    

6 个答案:

答案 0 :(得分:4)

作为使用字符串进行比较的替代方法,我建议使用算术分别得到数字的十位和一位数字:

tens = num // 10 # the // operator does integer division
ones = num % 10  # the % operator finds the modulus

如果您使用usernumlotterynum值执行此操作,则可以了解它们的匹配方式。由于这听起来像是家庭作业,我会把细节留给你!

答案 1 :(得分:4)

您可以使用set。

lottery_set = set('%02d' % lottery_num)
user_set = set('%02d' % user_num)
if lottery_num == user_num:
    print('10000')
elif lottery_set == user_set:
    print('3000')
elif lottery_set.intersection(user_set):
    print('1000')
else:
    print('0')

答案 2 :(得分:3)

就一个想法而言,我会说将你的数字转换成一个字符串,然后使用该字符串中的字符来确定获胜的组合(例如:相同的字符相同的位置,相同的字符不同的位置等)< / p>

答案 3 :(得分:2)

这里有可能帮到你的东西, 你可以这样做:

if(str(lottery_num)[0] == str(usernum)[0]):
    print "True"

内置str函数会将整数转换为字符串。在上面的示例中,它将允许您将字符串的第一个元素与用户字符串的第一个元素进行比较。使用这种访​​问,您可以通过这种方式解决问题。 0表示这种情况下的第一个元素,就像char数组那样。

答案 4 :(得分:1)

您需要隔离数字,以便可以相互独立地进行比较。

一种简单的方法是将数字转换为字符串,然后比较各个字符。

答案 5 :(得分:0)

这是完成作业:)

import random
import time

##Declare Variables
user_num=0
##lottery_num=random.randint(10,99)
lottery_num=12

##Input
print("Welcome to the Lottery Program!")
user_num=int(input("Please enter a two digit number: "))
print("Calculating Results.")
for i in range(3):
  time.sleep(1)
  print(".")

##Calc & Output
lottery_tens = lottery_num // 10
lottery_ones = lottery_num % 10

user_tens = user_num // 10
user_ones = user_num % 10

if lottery_num == user_num:
    print("All your numbers match in exact order! Your reward is $10,000!\n")
elif lottery_tens == user_ones and lottery_ones == user_tens:
    print("All your numbers match! Your reward is $3,000!\n")
elif lottery_tens == user_tens or lottery_ones == user_ones \
  or lottery_ones == user_tens or lottery_tens == user_ones:
    print("One of your numbers match the lottery. Your reward is $1,000!\n")
else:
    print("Your numbers don't match! Sorry!")

##Same as Calc & Output using Sets.
##lottery_set = set('%02d' % lottery_num)
##user_set = set('%02d' % user_num)
##if lottery_num == user_num:
##    print("All your numbers match in exact order! Your reward is $10,000!\n")
##elif lottery_set == user_set:
##    print("All your numbers match! Your reward is $3,000!\n")
##elif lottery_set.intersection(user_set):
##    print("One of your numbers match the lottery. Your reward is $1,000!\n")
##else:
##    print("Your numbers don't match! Sorry!")