《python语言程序设计》(2018版)第4章编程题15题 6月8日随机3位数中奖系统编写

我今天做到第4章的15题,应该说从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字段后,就停止了运行。 所以我做了更改,将顺序改为文章开头你们看到的样子。 非常感谢大家的观看 希望大家多提意见。谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

谢谢兄弟姐妹们。多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值