When I run my code (below), it comes up with the error NameError: global name 'w' is not defined
when it comes to opening the file.
当我运行我的代码(下面)时,它会出现错误NameError:在打开文件时,全局名称“w”没有定义。
I do not know why it is doing that because 'w' is how it writes the file, it is not a variable?
我不知道它为什么这么做,因为“w”是它写文件的方式,它不是一个变量?
import random
import math
def var():
strength = 10
skill = 10
dice4 = 0
dice12 = 0
dice_score = 0
character_name = str(input("Please enter your characters name: "))
skill_func(strength, skill, dice4, dice12, character_name, dice_score)
def skill_func(strength, skill, dice4, dice12, character_name, dice_score):
print(character_name + "'s attributes are being generated! ... ")
dice4, dice12 = random.randrange(1,4), random.randrange(1,12)
dice_score = dice12/dice4
dice_score = math.floor(dice_score)
skill = skill + dice_score
strength_func(strength, skill, dice4, dice12, character_name, dice_score)
def strength_func(strength, skill, dice4, dice12, character_name, dice_score):
dice4, dice12 = random.randrange(1,4), random.randrange(1,12)
dice_score = dice12/dice4
dice_score = math.floor(dice_score)
strength = strength + dice_score
file(strength, skill, dice4, dice12, character_name)
def file(strength, skill, dice4, dice12, character_name):
file = open("N:\Controlled Assessment - James Barham\Task Two\attributes.txt", w)
file.writelines(character_name + " - Strength = " + str(strength) + ", Skill = " + str(skill))
var()
1 个解决方案
#1
4
This:
这样的:
file = open("N:\Controlled Assessment - James Barham\Task Two\attributes.txt", w)
Should be this:
应该是这样的:
file = open("N:\Controlled Assessment - James Barham\Task Two\attributes.txt", "w")
#1
4
This:
这样的:
file = open("N:\Controlled Assessment - James Barham\Task Two\attributes.txt", w)
Should be this:
应该是这样的:
file = open("N:\Controlled Assessment - James Barham\Task Two\attributes.txt", "w")