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

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

[复制链接]

5

主题

36

回帖

-182

积分

限制会员

积分
-182
发表于 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()
回复

使用道具 举报

0

主题

13

回帖

518

积分

高级会员

积分
518
发表于 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)
回复

使用道具 举报

8

主题

73

回帖

2767

积分

金牌会员

河豚绿植

积分
2767
发表于 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

主题

44

回帖

1807

积分

金牌会员

积分
1807
发表于 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)
回复

使用道具 举报

20

主题

118

回帖

2853

积分

版主

积分
2853
发表于 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())
回复

使用道具 举报

3

主题

69

回帖

78

积分

版主

积分
78
发表于 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)


回复

使用道具 举报

1

主题

15

回帖

2396

积分

金牌会员

积分
2396
发表于 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)


回复

使用道具 举报

6

主题

41

回帖

3268

积分

论坛元老

积分
3268
发表于 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) #打印变量



回复

使用道具 举报

1

主题

7

回帖

-75

积分

限制会员

积分
-75
发表于 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)


回复

使用道具 举报

2

主题

15

回帖

1443

积分

金牌会员

积分
1443
发表于 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)
回复

使用道具 举报

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

本版积分规则

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