6月8日随机3位数中奖系统编写

今天继续分享python 做题

我今天做到第4章的14题,应该说从6月7日到今天上午。我一直在做着道题。随机产生3个数字,然后我们猜全中给10000,数字猜中但顺序不对给3000.猜对一个数给1000

该程序原为随机出2位数,然后猜2位数。习题改成了3位数。

import random
lottery = random.randint(0, 999)
print(lottery)
guess = eval(input("Enter your lottery pick (Three digits): "))

lotteryD1 = lottery // 100
# print(lotteryD1)
lotteryDS = lottery % 100   此处上一次练习时并没有注意到应该取100%,这个算上一次的一个错误
# print(lotteryDS)
lotteryD2 = lotteryDS // 10
# print(lotteryD2)
lotteryD3 = lottery % 10
# print(lotteryD3)


guessD1 = guess // 100
# print(guessD1)
guessDS = guess % 100
# print(guessDS)
guessD2 = guessDS // 10
# print(guessD2)
guessD3 = guessDS % 10
# print(guessD3)
 

#接着下面这个地方是我纠结了将近一天的地方。 首先我先从好理解的出一个数这方面考虑,编出了

“elif guessD1 or guessD2 or guessD3 == lotteryD1 or lotteryD2 or lotteryD3:”这是我第2次刷题时的新想法,不错。
if guess == lottery:
    print("Lucky is 10000 ")
elif (guessD2 == lotteryD1 and guessD1 == lotteryD2 and guessD3 == lotteryD3):
      print("Come guest good 3000")
elif (guessD2 == lotteryD1 and guessD3 == lotteryD2 and guessD1 == lotteryD3):
      print("Come guest good 3000")
elif (guessD1 == lotteryD1 and guessD3 == lotteryD2 and guessD2 == lotteryD3):
      print("Come guest good 3000")
elif (guessD3 == lotteryD1 and guessD2 == lotteryD2 and guessD1 == lotteryD3):
      print("Come guest good 3000")
elif (guessD3 == lotteryD1 and guessD1 == lotteryD2 and guessD2 == lotteryD3):
      print("Come guest good 3000")
elif guessD1 or guessD2 or guessD3 == lotteryD1 or lotteryD2 or lotteryD3:
    print("More GoGo is 1000")

接着面对数字对但顺序不对。我在此想了很长时间。然后相出了一下的公式。但是结果往往出现了2种情况

 

764  #此处是我打印出了随机结果以便测试用

Enter your lottery pick (Three digits): 467  #提示用户输入

More GoGo is 1000  #奖金提示。

大家看到了数字全对,顺序不对应该是3000.但是这里不论顺序不对还是只出一个数,他都执行的是“or”。我首先怀疑的是我写的and  and 这样的公式不对。为了测试我在程序的下方有写了以下几句

 

# 测试用
# print(guessD2 == lotteryD1 and guessD1 == lotteryD2 and guessD3 == lotteryD3)
# print(guessD1 == lotteryD1 and guessD3 == lotteryD2 and guessD2 == lotteryD3)
# print(guessD3 == lotteryD1 and guessD2 == lotteryD2 and guessD1 == lotteryD3)
# print(guessD2 == lotteryD1 and guessD3 == lotteryD2 and guessD1 == lotteryD3)

 

经过这些语句的测试,我发现我的设想并没有错。我写的也没有错。那是什么道理呢。直到刚才。我才想到是不是顺序。Python是从上往下执行。那么我把

 #  elif guessD1 or guessD2 or guessD3 == lotteryD1 or lotteryD2 or lotteryD3:
  #  print("More GoGo is 1000")

放在了首位。后面则是判定数字不是顺序的and 造成计算机只运行到or字段后,就停止了运行。

所以我做了更改,将顺序改为文章开头你们看到的样子。

 

非常感谢大家的观看 希望大家多提意见。谢谢



 

posted @ 2020-06-08 14:04  yogaPandas  阅读(228)  评论(0编辑  收藏  举报