在计算和返回后,不会打印变量

时间:2014-04-10 15:31:38

标签: python python-3.x

import time,random,math,sys
from random import *

Choice = ["1","2","3","4","5","6","7"]
PlayerList = []
str1 = 0
str2 = 0
ski1 = 0
ski2 = 0
diffstr = 0
diffski = 0
strmod = 0
skimod = 0
name1 = ""
name2 = ""
Roll1 = 0
Roll2 = 0
death1 = False
death2 = False
name1modstr = 0
name2modstr = 0
name1modski = 0
name2modski = 0

def Start():
    name1 = input('''
Choose Character 1's name: ''')
    str1 = int(input('''
Choose Character 1's strength: '''))
    ski1 = int(input('''
Choose Character 1's skill: '''))
    name2 = input('''
Choose Character 2's name: ''')
    str2 = int(input('''
Choose Character 2's strength: '''))
    ski2 = int(input('''
Choose Character 2's skill: '''))
    CalcDiff(str1,str2,ski1,ski2,name1,name2)
    return(str1,str2,ski1,ski2,name1,name2)


def CalcDiff(str1,str2,ski1,ski2,name1,name2):
    print(str1,str2,ski1,ski2)
    if str1 >= str2:
        diffstr = str1 - str2
    if str2 > str1:
       diffstr = str2 - str1
    if ski1 >= ski2:
        diffski = ski1 - ski2
    if ski2 > ski1:
       diffski = ski2 - ski1
    print(diffstr, "Strength Difference")
    print(diffski, "Skill Difference" )
    CalcModify(diffstr,diffski)
    return(diffstr,diffski,name1,name2)


def CalcModify(diffstr,diffski):
    strmod = diffstr//5
    skimod = diffski//5
    print(strmod, "Strength modifier")
    print(skimod, "Skill modifier")
    DiceRoll(strmod,skimod,name1,name2)
    return(strmod,skimod,name1,name2)

def DiceRoll(strmod,skimod,name1,name2):
    print("Dice rolling for: ", name1)
    Roll1 = randint(1,6)
    time.sleep(2)
    print(name1, " rolled ", Roll1)
    print("Dice rolling for: ", name2)
    Roll2 = randint(1,6)
    time.sleep(2)
    print(name2," rolled ", Roll2)
    RollDiff(Roll1,Roll2,name1,name2)
    return(Roll1,Roll2,name1,name2)

def RollDiff(Roll1,Roll2,name1,name2):
    if Roll1 == Roll2:
        print("No changes made...")
    if Roll1 > Roll2:
        print(name1, " has the higher score. Strength and skill modifiers will be added...")
        name1modstr = str1 + strmod
        name1modski = ski1 + skimod
        print(name1, "'s new strength is ", name1modstr)
        print(name1, "'s new skill is ", name1modski)
        print(name2, " scored the lower score. Strength and skill modifiers will be subtracted...")
        name2modstr = str2 - strmod
        name2modski = ski2 - skimod
        if name2modski < 0:
            name2modski = 0
        if name2modstr < 0:
            death2 = True
        else:
            print(name2, "'s new strength is ", name2modstr)
            print(name2, "'s new skill is ", name2modski)
        return(name1modstr,name1modski,name2modstr,name2modski,name1,name2)
    if Roll1 < Roll2:
        print(name2, " has the higher score. Strength and skill modifiers will be added...")
        name2modstr = str2 + strmod
        name2modski = ski2 + skimod
        print(name2, "'s new strength is ", name2modstr)
        print(name2, "'s new skill is ", name2modski)
        print(name1, " scored the lower score. Strength and skill modifiers will be subtracted...")
        name1modstr = str1 - strmod
        name1modski = ski1 - skimod
        if name1modski < 0:
            name1modski = 0
        if name1modstr < 0:
            death1 = True
        else:
            print(name1, "'s new strength is ", name1modstr)
            print(name1, "'s new skill is ", name1modski)
        return(name1modstr,name1modski,name2modstr,name2modski)






while death1:
    print(name1, "died...")
    death1 = False

while death2:
    print(name2, "died...")
    death2 = False



while True:
    menu = input('''
[1] Start
[2] Create a character
[3] Print character list
[4] Load Character
[5] Save Character
[6] Clear Character Files
[7] Quit

Choice = ''')
    if menu == "1":
        Start()
    if menu == "2":
        print('''
Create a character''')
    if menu == "3":
        print('''
Print character list''')
    if menu == "4":
        print('''
Load Character (TESTING ONLY)''')
    if menu == "5":
        print('''
Save Character''')
    if menu == "6":
        print('''
Clear Character Files''')
    if menu not in Choice:
        print('''
Please choose a valid number''')

当DiceRoll&#39;和&#39; RollDiff&#39;功能运行&#39; name1&#39;和&#39; name2&#39;没有打印,即使它们被退回。还有&#39; name1 / 2modstr / ski&#39;也没有打印,即使计算完成,它们都返回0.没有IDLE错误,所以据我所知这是一个我未能看到的逻辑错误。

2 个答案:

答案 0 :(得分:0)

将打印功能更改为此类

print("{} rolled {}".format(name1, roll1))

答案 1 :(得分:0)

Start()函数中的所有变量都是该函数的本地变量。当你return时,你可以获得这些变量的值,但前提是你在另一个函数的arg中返回它们。

本地范围示例:

>>> bar = 'rab'   #set var bar to 'rab'
>>> def foo(bar): #function that returns
    bar = 'bar'   #var bar as string 'bar'
    return bar
>>> foo(bar)      #call foo() function
'bar'             #returns the value of bar inside the scope of foo()
>>> bar           #ask for the bar var made earlier
'rab'             #still set to 'rab'

你想要什么,将函数作为参数调用(返回值出现!):

>>> def foo2(arg): #this is like your DiceRoll function
    print(arg)     
>>> foo2(foo(bar)) #when you call it and pass foo(bar) as an arg
bar                #it prints the return of foo(bar)

编辑:它在DiceRoll中无效的原因是因为您在调用DiceRoll并分配该变量的函数中留下了对该变量的引用&# 39; s对第一个创建的实例的值(即''0,在这种情况下)。考虑这个示例并将逻辑应用于您的代码。

a = 'a'
b = 0

def start():
    a = 'string'
    b = 1
    nextFunc(a, b)

def nextFunc(a, b):
    print(a, b)      #a,b exist as 'string' and 1 is this func,
    c = 0            #but when you call lastFunc,  
    d = 1            #and don't pass a,b, they will
    lastFunc(c, d)   #show up in lastFunc as 'a' and 0

def lastFunc(c, d):
    print(a, b, c, d)

start()