[英]In python putting 2 variables in a print
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')
另外,關於如何使代碼更短或更有效的任何建議。 謝謝
字符串格式化應在您需要的任何情況下都有效
message = "Character x, your strength: {} \n".format(characterXStrength)
print(message)
請注意,print本質上已經在其中添加了換行符,因此如果要使用兩個字符,則僅包括\\n
。
print('Character one , your strength:',str(characterOneStrength) + '\n')
快要到了:應該
print("character one your strength is:"+str(characterOneStrength)+"
你需要兩面都加號
這是擴展版本-進行追溯,您應該學到很多東西:
import random
import sys
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
rng = xrange
else:
# Python 3.x
inp = input
rng = range
NAMES = [
"Akhirom", "Amalric", "Aratus", "Athicus", "Bragoras", "Cenwulf", "Chiron",
"Crassides", "Dayuki", "Enaro", "Farouz", "Galbro", "Ghaznavi", "Godrigo",
"Gorulga", "Heimdul", "Imbalayo", "Jehungir", "Karanthes", "Khossus"
]
NUM_CHARS = 5
OUTPUT = "CharacterAttributes.txt"
def roll(*args):
"""
Return the results of rolling dice
"""
if len(args) == 1: # roll(num_sides)
sides, = args
return random.randint(1, sides)
elif len(args) == 2: # roll(num_dice, num_sides)
num, sides = args
return sum(random.randint(1, sides) for _ in rng(num))
else:
raise TypeError("roll() takes 1 or 2 arguments")
class Character:
def __init__(self, name=None):
if name is None:
name = inp("Please input character name: ").strip()
self.name = name
self.strength = 10 + roll(10) // roll(4)
self.skill = 10 + roll(10) // roll(4)
def __str__(self):
return "{}: strength {}, skill {}".format(self.name, self.strength, self.skill)
def main():
# generate names (assumes len(NAMES) >> NUM_CHARS)
names = random.sample(NAMES, NUM_CHARS - 1)
# make a character for each name
chars = [Character(name) for name in names]
# add an unnamed character (prompt for name)
chars.append(Character())
# write characters to file
with open(OUTPUT, "w") as outf:
for ch in chars:
outf.write("{}\n".format(ch))
if __name__=="__main__":
main()
並且,出於興趣的考慮,這是強度和技能屬性的期望值分布:
10: ****** (15.0%)
11: ********** (25.0%)
12: ********* (22.5%)
13: ***** (12.5%)
14: *** ( 7.5%)
15: ** ( 5.0%)
16: * ( 2.5%)
17: * ( 2.5%)
18: * ( 2.5%)
19: * ( 2.5%)
20: * ( 2.5%)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.