找回密码
 中文实名注册
楼主: admin

【Python作业】第四单元 第4课 黄金矿工 全局变量

[复制链接]
回帖奖励 1280 金钱 回复本帖可获得 40 金钱奖励! 每人限 1 次

4

主题

44

帖子

1536

积分

金牌会员

Rank: 6Rank: 6

积分
1536
QQ
发表于 2022-1-23 10:02:29 | 显示全部楼层

回帖奖励 +40 金钱

[Python] 纯文本查看 复制代码
import random

total = 0


def cal():
    global total
    box = random.choice(['石头','黄金','钻石','炸弹'])
    if box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 100
    elif box == '炸弹':
        total = total - 200
    else:
        total = total +500


cal()
print(total)[color=Lime][/color]
回复

使用道具 举报

3

主题

48

帖子

1009

积分

金牌会员

Rank: 6Rank: 6

积分
1009
发表于 2022-1-23 10:01:53 | 显示全部楼层

回帖奖励 +40 金钱

import random
total = 0
def cal():
    global total
    box = random.choice(['石头','黄金','钻石','炸弹'])
    if box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 100
    elif box == '炸弹':
        total = total - 200
    else:
        total = total + 500
cal()
print(total)
回复

使用道具 举报

3

主题

36

帖子

1716

积分

金牌会员

Rank: 6Rank: 6

积分
1716
发表于 2022-1-23 09:52:41 | 显示全部楼层

回帖奖励 +40 金钱

import random
total = 0
def cal():
    global total
    box = random.choice(['石头','黄金','钻石','炸弹'])
    if box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 100
    elif box == '炸弹':
        total = total - 200
    else:
        total = total + 500
cal()
print(total)
回复

使用道具 举报

6

主题

110

帖子

3308

积分

论坛元老

Rank: 8Rank: 8

积分
3308
发表于 2022-1-22 15:52:24 | 显示全部楼层

回帖奖励 +40 金钱


import random
total = 0
def cal():
    global total
    box = random.choice(['石头', '黄金', '钻石','骨头'])
    if box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 100
    elif box == '钻石':
        total = total + 500
    else:
        total = total - 20
cal()
print(total)
回复

使用道具 举报

2

主题

30

帖子

715

积分

高级会员

Rank: 4

积分
715
发表于 2022-1-22 15:24:41 | 显示全部楼层

回帖奖励 +40 金钱

import random


total = 0


def cal():
    global total
    box = random.choice(['石头', '黄金', '钻石'])
    if box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 5656
    else:
        total = total + 234678475784


cal()
print(total)
回复

使用道具 举报

0

主题

13

帖子

557

积分

高级会员

Rank: 4

积分
557
发表于 2022-1-22 15:19:31 | 显示全部楼层

回帖奖励 +40 金钱

import random


total=0


def cal():
    global total
    box=random.choice(['石头','黄金','钻石','猪'])
    if box == '石头':
        total=total+20
    elif box == '黄金':
        total=total+100
    elif box == '钻石':
        total = total+500
    else:
        total = total-100

cal()
print(total)
回复

使用道具 举报

1

主题

20

帖子

867

积分

高级会员

Rank: 4

积分
867
发表于 2022-1-22 15:18:54 | 显示全部楼层

回帖奖励 +40 金钱

import random
total = 0
def cal():
    global total
    box = random.choice(['狗骨头','石头','黄金','钻石'])
    if box == '狗骨头':
        total = total - 20
    elif box == '石头':
        total = total + 20
    elif box == '黄金':
        total = total + 100
    else:
        total = total + 500
cal()
print(total)

回复

使用道具 举报

4

主题

43

帖子

2436

积分

金牌会员

Rank: 6Rank: 6

积分
2436
发表于 2022-1-22 15:15:44 | 显示全部楼层

回帖奖励 +40 金钱

import random
total = 0



def cal():
    global total
    s = ['石头', '黄金', '钻石', '狗骨头', '陈校长']
    box = random.choice(s)
    if box=='石头':
        total+=20
    elif box=='黄金':
        total+=100
    elif box =='钻石':
        total += 500
    elif box =='狗骨头':
        total -= 20
    else:
        total *= 2
    print(total)

回复

使用道具 举报

2

主题

46

帖子

1437

积分

金牌会员

Rank: 6Rank: 6

积分
1437
发表于 2022-1-22 15:12:17 | 显示全部楼层

回帖奖励 +40 金钱

import random


total = 0


def cal():
    global total
    while True:
        box = random.choice(['石头','钻石','黄金','狗骨头'])
        print(box)
        if box == '石头':
            total = total + 20
        elif box == '钻石':
            total = total + 500
        elif box == '黄金':
            total = total + 100
        else:
            total = total - 100
        print(total)

cal()
回复

使用道具 举报

5

主题

69

帖子

2030

积分

金牌会员

Rank: 6Rank: 6

积分
2030
发表于 2022-1-22 15:08:54 | 显示全部楼层

回帖奖励 +40 金钱

import random
total= 0


def cal():
    global total
    ore_list = ['石头','黄金','钻石','狗骨头']
    box = random.choice(ore_list)
    if box == '石头':
        total=total+20
    elif box == '黄金':
        total = total+100
    elif box == '钻石':
        total = total+500
    else :
        total = total-20












cal()
print(total)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-4-29 03:00 , Processed in 0.063050 second(s), 55 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表