【问题标题】:Compare two digit numbers for a lottery program in Python比较Python中彩票程序的两位数字
【发布时间】:2013-03-03 03:12:33
【问题描述】:

我在我的 Python 课程中有一项作业正在努力解决。

程序

这是基本前提: 彩票计划。程序随机生成一个两位数,提示用户输入两位数,根据以下规则判断用户是否中奖:

  1. 如果用户的输入与确切顺序中的彩票匹配,则奖金为 10,000 美元
  2. 如果用户输入的所有数字都与彩票号码中的所有数字匹配,则奖金为 1,000 美元
  3. 如果用户输入的一位数字与彩票号码中的一位数字匹配,则奖金为 1,000 美元

基本格式

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

然后通过一系列嵌套 if 将数字与 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!")

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

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

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

    如果您同时使用 usernum 和 lotterynum 值,则可以找出它们的匹配方式。因为这听起来像是一项家庭作业,所以我将把细节留给你!

    【讨论】:

    • @litebread:一般来说,要获得以 10 为底的数字的第 n 位,您可以:(num // (10**n)) % 10
    • 谢谢,我也想出了如何处理这个问题!你真棒! +1
    【解决方案2】:

    你可以使用 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')
    

    【讨论】:

    • 这完美!非常非常感谢你! +1 先生!
    • +1 使用适当的数据结构让生活更轻松
    • 就像我说的,这很棒!将来我会更多地研究套装。 ATM 这对我来说很新,我不知道它是如何工作的,但它工作得很好。再次感谢您!
    【解决方案3】:

    就一个想法而言,我会说将您的号码转换为字符串,然后使用该字符串中的字符来确定获胜组合(例如:相同的字符相同的位置,相同的字符不同的位置等)

    【讨论】:

    • 我还不知道该怎么做。我们还没有学过字符串转换。如果您能指导我到一个地方获得教程/说明,那就太好了。感谢您的帮助!
    • stackoverflow.com/questions/961632/…(将 int 转换为字符串)
    • mail.python.org/pipermail/tutor/2004-November/033569.html(从python中的字符串中提取一块/字符)
    • 所以我和教授谈过了,他暗示要使用 //(见上文),当我提到字符串时,他说这是额外的工作,在这种情况下并不是真正必要的。此外,我们还没有在课堂上学到这一点,所以我试图保持话题。感谢您的帮助,我仍然非常感谢您的努力和帮助!
    【解决方案4】:

    这里有一些可以帮助你的东西, 你可以这样做:

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

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

    【讨论】:

    • 这看起来很酷。试图弄清楚atm。谢谢!
    【解决方案5】:

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

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

    【讨论】:

    • 我不知道该怎么做。你有没有机会给我看一个教程/文档页面,这样我就可以学习如何做到这一点?谢谢!
    【解决方案6】:

    这是完整的作业:)

    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!")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多