《python语言程序设计》第5章 第34题利用循环显示两个数之间的不同

在这里插入图片描述

这个是34题的内容。我理解不了就改成 了用循环的方式随机出数和计算机随机出数进行比较。结果输多。根本赢不了,程序如下,先上结果

在这里插入图片描述

看到了吗最后输很多次

我设计的题如下,祝大家玩的开心

# 5.34

import random

count_win = 0
count_win_draw = 0
count_lose = 0
count_draw = 0
for guess in range(0, 100):
    lottery = random.randint(0, 99)

    lotteryDigit1 = lottery // 10
    lotteryDigit2 = lottery % 10

    guessDigit1 = guess // 10
    guessDigit2 = guess % 10

    print("The lottery number is", lottery)

    if guess == lottery:
        print("Exact match: you win $10,000")
        count_win += 1
    elif (guessDigit2 == lotteryDigit1 and
          guessDigit1 == lotteryDigit2):
        print("Match all digits: you win $ 3,000")
        count_win_draw += 1

    elif (guessDigit1 == lotteryDigit2
          or guessDigit2 == lotteryDigit1
          or guessDigit2 == lotteryDigit2):
        print("Match one digit: you win $1,000")
        count_draw += 1

    else:
        print("Sorry, no match")
        count_lose += 1

print(f"This lottery match win {count_win}")
print(f"This lottery match draw {count_draw}")
print(f"This lottery match lose {count_lose}")
print(
    f"This match last money{(count_win * 10000) + (count_win_draw * 3000) + (count_draw * 1000) - (count_lose * 1500)}")


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

电饭叔

谢谢各位兄弟们的关注,谢谢破费

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

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

打赏作者

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

抵扣说明:

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

余额充值