找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3212|回复: 2

[代码与实例] python2 变量作用域问题

2

主题

5

帖子

5

积分

贫民

积分
5
mylife18 发表于 2017-9-28 11:59:50 | 显示全部楼层 |阅读模式
本帖最后由 mylife18 于 2017-9-28 12:10 编辑

# -*- coding: utf-8 -*-

from sys import exit

first_sentence_property = "[1]"                                             # 此处 first_sentence_property = "[1]"
second_sentence_mood = "[2]"

first_sentence = "We don't read and write poetry because it's %s" % first_sentence_property + "."
second_sentence = "We read and write poetry because we are members of human being race. And human race is filled with %s" % second_sentence_mood + "."

print "Now let us make some choice for these sentences.\n>>>", first_sentence + "\n", second_sentence


def first_choice():
        
        global first_sentence_property                                                # 声明 global first_sentence_property
        print first_sentence_property                                                   # 此处已验证了 first_sentence_property = "[1]"
        
        global first_sentence
        
        while True:
                property = raw_input('>[1]-[boring, cute, confused]\n>')
        
                if property == "boring" :
                        print property
                        first_sentence_property = property                            # 此处将 first_sentence_property 赋值为 "boring"
                        print first_sentence_property                                     # 此处已验证了 first_sentence_property = "boring"
                        print "\n***\nHere is your anwser:", first_sentence   # 为何 first_sentence_property 不是 "boring"
                        end_choice()
                elif property == "cute":
                        second_choice()
                elif property == "confused":
                        end_choice()
                else:
                        print """
                        Your choice is out of range.
                        Please choose it in "boring, cute, confused"
                        """
        
        return property

                  
def second_choice():

        print "Good! let us explore another one."
        print """
        We read and write poetry because
        we are members of human being race.
        And human race is filled with [2]
        """

        while True:
        
                mood = raw_input('>[2]-[lazy, passion, angry]\n>')
        
                if mood == "lazy":
                        end_choice()
                elif mood == "passion":
                        print """
                        Good job! This gift is for you:
                        >>>>>>
                        Medicine, law, business, engineering:
                        these are noble pursuits and necessary to
                        sustain life. But the poetry...beauty, romance, love...
                        these are what we stay alive for.
                        """
                        exit()
                elif mood == "angry":
                        end_choice()
                else:
                        print """
                        !!!Your choice is out of range.
                        Please choose it in 'lazy, passion, angry'
                        """
        
def summarize_sentences():
        global first_sentence_property
        first_sentence_property = first_choice()
        print "***\nHere is your anwser:"
        return first_sentence
        
                        
def restart():
        print "\nDo you want to play again? 'y' or 'no' "
        while True:
                play_again = raw_input('>')
                if play_again == "y":
                        first_choice()
                elif play_again == "n":
                        exit()
                else:
                        print "!!!I don't understand!"
                        

def end_choice():
        print "ah...I think it can be better.\n***"
        # summarize_sentences()
        restart()
        
        
first_choice()



版本信息:Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

系统为:win7 64bit


开始变量 first_sentence_property 赋值为「 [1] 」

在函数 first_choice 中,我已把 first_sentence_property 声明 global,并重新赋值为「boring」了,且已验证其确实为「boring」

为何在输出 first_sentence 时,其中的 first_sentence_property 仍然为「 [1] 」,而不是「boring」,新手一枚望指导!


回复

使用道具 举报

0

主题

30

帖子

30

积分

贫民

积分
30
胡扶林 发表于 2017-9-28 14:18:54 | 显示全部楼层
first_sentence = "We don't read and write poetry because it's %s" % first_sentence_property + "." 这句话在编译之后 first_sentence 的值已经变成We don't read and write poetry because it's [1]. 了,后面你对first_sentence_property的操作不会对first_sentence有任何的影响。first_sentence的值当然不会有任何改变。 你想实现你的目的, 可以一开始的时候只设定first_sentence = "We don't read and write poetry because it's %s.",后面使用first_sentence的时候, 调用 first_sentence % first_sentence_property即可
回复 支持 反对

使用道具 举报

2

主题

5

帖子

5

积分

贫民

积分
5
mylife18  楼主| 发表于 2017-9-29 08:54:26 | 显示全部楼层
胡扶林 发表于 2017-9-28 14:18
first_sentence = "We don't read and write poetry because it's %s" % first_sentence_property + "." 这 ...

确实如此,感谢指正,我把它改写成了:
「print   first_sentence  +  " %s"   %   first_sentence_property」

似乎,python的赋值规则不适用于「变量内的变量」。
自己都惯性地认为赋值适用于此了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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