【发布时间】:2014-02-26 18:43:08
【问题描述】:
import random
characterNameOne=str(input("Please input first character's name"))
characterNameTwo=str(input("Please input second character's name"))
print("The characters have 2 attributes : Strength and Skill")
dieOne = random.randint(1,4)
dieTwo = random.randint(1,12)
print ("A 12 and 4 sided dice are rolled")
print("Each character is set to 10")
characterOneStrength = 10
characterOneSkill = 10
characterTwoStrength = 10
characterTwoSkill = 10
DivisionValue=round((dieTwo/dieOne),0)
print("The number rolled on the 12 sided dice is divided by the number rolled on the 4 sided dice")
characterOneStrength += DivisionValue
characterOneSkill += DivisionValue
characterTwoStrength += DivisionValue
characterTwoSkill += DivisionValue
print ("The value of the divided dice is added to the character's attributes")
print('Character one , your strength:',str(characterOneStrength) + '\n')
print('Character one, your strength:',str(characterOneSkill) + '\n')
print('Character two, your strength:',str(characterTwoStrength) + '\n')
print('Character two, your strength:' ,str(characterTwoSkill) + '\n')
fileObj = open("CharacterAttributes.txt","w")
fileObj.write('str(CharacterNameOne),your strength:' + str(characterOneStrength) + '\n')
fileObj.write('str(characterNameOne), your skill:' + str(characterOneSkill) + '\n')
fileObj.write('str(characterNameTwo),your strength:' + str(characterTwoStrength) + '\n')
fileObj.write('str(characterNameTwo), your skill:' + str(characterTwoSkill) + '\n')
fileObj.close()
您好,我将这段代码作为学校受控评估的草稿编写。任务是:
在确定游戏角色的某些特征时,骰子组合上的数字用于计算某些属性。
其中两个属性是力量和技能。
在游戏开始时,当创建角色时,会掷出一个 4 面骰子和一个 12 面骰子,以使用以下方法为每个角色确定每个属性的值:
每个属性最初设置为 10。 12 面骰子的分数除以 4 面骰子的分数并向下取整。 该值被添加到初始值。 对每个字符的每个属性重复此过程。 使用合适的算法描述这个过程。
编写并测试代码以确定一个字符的这两个属性,并将两个字符的示例数据(包括合适的名称)存储在一个文件中。
我想知道如何在这段代码中添加具有用户输入的字符名称的变量。我试过了,但是不行:
print('第一个字符,你的实力:',str(characterOneStrength) + '\n')
另外,关于如何使代码更短或更高效的任何建议。谢谢
【问题讨论】:
-
引发了什么异常?如果您希望解决问题的这一部分,也可以在 CodeReview 上提出。
标签: python