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

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

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

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)
回复

使用道具 举报

4

主题

47

帖子

735

积分

高级会员

Rank: 4

积分
735
发表于 2022-1-22 11:39:43 | 显示全部楼层

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

主题

45

帖子

1215

积分

金牌会员

Rank: 6Rank: 6

积分
1215
发表于 2022-1-22 11:35:46 | 显示全部楼层

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

使用道具 举报

7

主题

62

帖子

1128

积分

金牌会员

Rank: 6Rank: 6

积分
1128
发表于 2022-1-22 11:35:14 | 显示全部楼层

回帖奖励 +40 金钱

本帖最后由 陈晓帅 于 2022-1-22 11:49 编辑

import random

total = 0


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

论坛我最帅
回复

使用道具 举报

0

主题

29

帖子

86

积分

注册会员

Rank: 2

积分
86
发表于 2022-1-22 11:34:35 | 显示全部楼层

回帖奖励 +40 金钱

import random
total = 0

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

使用道具 举报

2

主题

26

帖子

1558

积分

金牌会员

Rank: 6Rank: 6

积分
1558
发表于 2022-1-9 15:28:31 | 显示全部楼层

回帖奖励 +40 金钱

import random
a=random.randint(1,100000000000)
while True:
    g=input('输入1到10000000000000的一个数')
    g=int(g)
    if g>a:
        print('大')
    elif g<a:
            print('小')
    else:
        print('对')
        break
回复

使用道具 举报

3

主题

73

帖子

1559

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1559
发表于 2022-1-9 15:26:00 | 显示全部楼层

回帖奖励 +40 金钱

import random
a=random.randint(1,100000000000)
while True:
    g=input('输入1到10000000000000的一个数')
    g=int(g)
    if g>a:
        print('大')
    elif g<a:
            print('小')
    else:
        print('对')
        break
回复

使用道具 举报

4

主题

83

帖子

3868

积分

论坛元老

Rank: 8Rank: 8

积分
3868
发表于 2022-1-9 10:21:45 | 显示全部楼层

回帖奖励 +40 金钱

[Python] 纯文本查看 复制代码
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-5-2 07:20 , Processed in 0.042252 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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