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

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

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

9

主题

59

帖子

1585

积分

金牌会员

Rank: 6Rank: 6

积分
1585
发表于 2022-1-22 15:05:43 | 显示全部楼层

回帖奖励 +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 + 234
    print(total)
cal()
回复

使用道具 举报

1

主题

24

帖子

899

积分

高级会员

Rank: 4

积分
899
发表于 2022-1-22 15:03:28 | 显示全部楼层

回帖奖励 +40 金钱

[C++] 纯文本查看 复制代码
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)
回复

使用道具 举报

10

主题

153

帖子

3074

积分

论坛元老

河豚绿植

Rank: 8Rank: 8

积分
3074
发表于 2022-1-22 15:02:37 | 显示全部楼层

回帖奖励 +40 金钱

[Python] 纯文本查看 复制代码
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)
回复

使用道具 举报

15

主题

96

帖子

2131

积分

金牌会员

Rank: 6Rank: 6

积分
2131
发表于 2022-1-22 15:01:50 | 显示全部楼层

回帖奖励 +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)
回复

使用道具 举报

23

主题

188

帖子

3518

积分

版主

Rank: 7Rank: 7Rank: 7

积分
3518
发表于 2022-1-22 14:54:57 | 显示全部楼层

回帖奖励 +40 金钱

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

使用道具 举报

4

主题

134

帖子

5209

积分

版主

Rank: 7Rank: 7Rank: 7

积分
5209
发表于 2022-1-22 14:54:26 | 显示全部楼层
import random
def cal():
    global total
    total = 0
    ore_list = ['石头','黄金','钻石','狗骨头'
    box = random.choice(ore_list)
    if box == '狗骨头':
        total -= 20
    elif box == '石头':
        total += 20
    elif box == '黄金':
        total += 100
    else:
        total += 500


cal()
print(total)


回复

使用道具 举报

2

主题

24

帖子

2519

积分

金牌会员

Rank: 6Rank: 6

积分
2519
发表于 2022-1-22 11:49:02 | 显示全部楼层

回帖奖励 +40 金钱

import random

total = 0

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

cal()
print(total)


回复

使用道具 举报

8

主题

80

帖子

3559

积分

论坛元老

Rank: 8Rank: 8

积分
3559
发表于 2022-1-22 11:40:44 | 显示全部楼层

回帖奖励 +40 金钱

本帖最后由 许睿辰 于 2022-1-22 11:50 编辑

import random #使用random模块
total = 0 #设置变量
def cal(): #创建函数
    global total #设置为全局变量
    box = random.choice(['石头', '钻石', '黄金']) #创建一个列表并随机选择一个
    if box == '石头': #判断如果是石头
        total = total + 20 #那么变量total加20
    elif box == '黄金 ': #判断如果是黄金
        total = total + 100 #那么变量total加100
    else: #除非(也只有钻石了)
         total = total + 500 #那么变量total加500
cal() #使用函数
print(total) #打印变量



回复

使用道具 举报

2

主题

18

帖子

2043

积分

金牌会员

Rank: 6Rank: 6

积分
2043
发表于 2022-1-22 11:40:32 | 显示全部楼层

回帖奖励 +40 金钱

import random


total = 0


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


回复

使用道具 举报

3

主题

18

帖子

1484

积分

金牌会员

Rank: 6Rank: 6

积分
1484
发表于 2022-1-22 11:40:09 | 显示全部楼层

回帖奖励 +40 金钱

import random

total = 0

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

cal()
print(total)
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 05:02 , Processed in 0.048237 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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