Compare two digit numbers for a lottery program in Python
在我的Python类中,我有一个很难完成的任务。
程序以下是基本前提:彩票计划。程序随机生成一个两位数,提示用户输入一个两位数,并根据以下规则确定用户是否获胜:
基本格式
基本上,我使用randint生成一个两位数的数字(比如说它生成58个)。然后用户输入相同长度的数字(尽管没有指定,但是为了简单起见,我们假设数字是10到99)
然后通过一系列嵌套的ifs,将这些数字与3个结果和1个异常进行比较。
问题:我不知道如何用指定的方式比较数字。我知道所有的基本运算符,但在本例中,我看不到使用它们的方法(除了可以使用==的完全匹配的数字)。我在考虑一个数组(来自我的C/C++类),但是我不确定如何在这里实现它。以下是我迄今为止所做的工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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! ") elif lottery_num== #WHAT DO HERE? print("All your numbers match! Your reward is $3,000! ") elif lottery_num== #WHAT DO HERE? print("One of your numbers match the lottery. Your reward is $1,000! ") else: print("Your numbers don't match! Sorry!") |
解决方案
在你们的帮助下,我终于明白了怎么做了!非常感谢你!这是对我所做的事感兴趣的人的全部任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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! ") elif lottery_tens == user_ones and lottery_ones == user_tens: print("All your numbers match! Your reward is $3,000! ") 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! ") 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! ") ##elif lottery_set == user_set: ## print("All your numbers match! Your reward is $3,000! ") ##elif lottery_set.intersection(user_set): ## print("One of your numbers match the lottery. Your reward is $1,000! ") ##else: ## print("Your numbers don't match! Sorry!") |
你可以用套装。
1 2 3 4 5 6 7 8 9 10 | 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 2 | tens = num // 10 # the // operator does integer division ones = num % 10 # the % operator finds the modulus |
如果同时使用
就一个想法而言,我想说把你的数字转换成一个字符串,然后使用该字符串中的字符来确定赢的组合(例如:相同的字符相同的位置,相同的字符不同的位置等)。
好吧,这是一些可以帮助你的东西,你可以这样做:
1 2 | if(str(lottery_num)[0] == str(usernum)[0]): print"True" |
内置的
您需要隔离这些数字,以便它们可以相互独立地进行比较。
一个简单的方法是将数字转换成字符串,然后比较各个字符。
以下是完整的任务:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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! ") elif lottery_tens == user_ones and lottery_ones == user_tens: print("All your numbers match! Your reward is $3,000! ") 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! ") 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! ") ##elif lottery_set == user_set: ## print("All your numbers match! Your reward is $3,000! ") ##elif lottery_set.intersection(user_set): ## print("One of your numbers match the lottery. Your reward is $1,000! ") ##else: ## print("Your numbers don't match! Sorry!") |