调用函数而不重置变量

2024-06-16 18:57:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个纸牌游戏,当它结束时,我希望人们能够玩更多的回合,但当它重放时,它必须再次通过变量。。重置分数。我正在寻找一种方法来修复它,而不需要一个全新的复杂代码块,我希望我只是错过了一个非常简单的修复。在

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports

import time


def play_game():

    # Variables

    acard = int()
    bcard = int()
    apoints = int()
    bpoints = int()

    # Repeat

    repeat = True

    # Hand out cards

    print 'Cards have been served'
    input('\nPress Enter to Reveal')

    # Cards Turn

    time.sleep(0.5)
    t = time.time()
    acard = int(str(t - int(t))[2:]) % 13
    print '\nYour card value is ' + str(acard)

    time.sleep(0.1)

    t = time.time()
    bcard = int(str(t - int(t))[2:]) % 13

    # Number Check & Point Assign

    time.sleep(2)
    if acard > 5:
        apoints += 1
        print '\nYour points have increased by one, your total is now ' \
            + str(apoints)
    if acard < 5:
        apoints -= 1
        print '\nYour points have decreased by one, your total is now ' \
            + str(apoints)
    if bcard > 5:
        bpoints += 1
        print '\nYour opponent got ' + str(bcard) \
            + ', their points have increased by one,\ntheir total is now ' \
            + str(bpoints)
    if bcard < 5:
        bpoints -= 1
        print '\nYour opponent got ' + str(bcard) \
            + ', their points have decreased by one,\ntheir total is now ' \
            + str(bpoints)

    # Card Reset

    bcard = 0
    acard = 0

    # Shuffle

    input('\nPress enter to shuffle deck')
    print '\nCards being shuffled'
    time.sleep(1)
    print '.'
    time.sleep(1)
    print '.'
    time.sleep(1)
    print '.'
    print 'Cards have been shuffled'
    global enda
    global endb
    enda = apoints
    endb = bpoints


# Loop

time.sleep(1)
answer = 'y'
while answer.lower() == 'y':
    play_game()
    answer = input('\nDo you wish to play again? (Y/N)')

# Scores

if enda > endb:
    print '\nYou Win!'
    print '\nScores'
    print 'You: ' + str(enda) + ' Opponent: ' + str(endb)

if endb > enda:
    print '\nYou Lost!'
    print '\nScores'
    print 'You: ' + str(enda) + ' Opponent: ' + str(endb)

Tags: iftimeishavesleepintprintstr
3条回答

函数的本质是在每次调用时初始化它们的变量。关键是他们每次的工作方式都是一样的。在

如果希望变量保持不变,并与函数关联,则使用对象。这也允许您将庞大的代码块分解成更小的函数。在

我可能会使用面向对象的方法来编写类级别的变量,但是如果您想让代码尽可能接近现在的代码,可以将score变量移到方法之外:

apoints = 0
bpoints = 0

def play_game():
    global apoints
    global bpoints
    ...

你能做的最简单的改变就是使这些变量“全局化”

例如:

a = 5

def myfunc():
    print a
    a = 7

myfunc()

将不起作用,因为在myfunc()中对a的赋值使Python认为您正在函数中声明第二个局部变量a。您可以通过执行以下操作来使用全局变量:

^{pr2}$

这显式声明您希望函数中对a的所有引用都引用全局定义的a。在

令人困惑的是,如果不在函数中赋值给a,那么它将把在函数外部定义的变量视为在函数范围内:

a = 5

def myfunc():
    print a

myfunc()

(这将打印数字5。)

编辑:建议使用类的另一个答案是一个提供许多好处的解决方案。在复杂的程序中使用许多全局变量会降低可维护性,因为强制这些变量共享一个名称空间,并且使一个函数可能对另一个函数可能产生的副作用不太明确,因为对于许多全局变量,您可能无法轻易地分辨出某个特定变量在哪里被更改。在

但是,如果您打算采用基于类的方法来解决您的问题,那么最好花点时间熟悉面向对象编程和设计的语言,因为您将要编写的代码的某些行为在您了解概念之前可能还不清楚。在

在您的代码中,将这些变量设为全局变量会改变这一部分:

def play_game():

    # Variables

    acard = int()
    bcard = int()
    apoints = int()
    bpoints = int()

为此:

acard = int()
bcard = int()
apoints = int()
bpoints = int()

def play_game():

    # Variables

    global acard
    global bcard
    global apoints
    global bpoints

# etc.

还有一个想法:使用

apoints = int()

因为您希望将变量“声明”为整数,可能并没有您认为的效果。在Python中可以这样做:

apoints = int()
print(apoints)

apoints = 5.0
print(apoints)

apoints = "My string"
print(apoints)

因此,将apoints设为整型并不能使变量永远都是整数。最好给变量指定一个显式值:

apoints = 0

Python变量类型的这个属性称为“duck typing”

http://en.wikipedia.org/wiki/Duck_typing

相关问题 更多 >