我有煩惱:Python;試圖在一個外部文件編寫與此代碼的變量
import random
import time
y = 0
while y <3:
x = 0
y += 1
numlist = []
while x <5:
x += 1
four = random.randint (1,4)
twelve = random.randint (1,12)
newint = 10 + twelve /four
round (newint)
numlist.append (newint)
strength = numlist[0]
skill = numlist[1]
health = numlist[2]
dexterity = numlist[3]
round (strength, 10)
round (skill, 10)
round (health, 10)
round (dexterity, 10)
print (strength)
print (skill)
print (health)
print (dexterity)
str(strength)
str(skill)
str(health)
str(dexterity)
doc = open ("character1", "w")
doc.write ("Strength is")
doc.write (strength)
doc.close()
輸出:
13.0
10.25
11.75
15.5
Traceback (most recent call last):
File "D:\LOCATION OF CODE, line 41, in <module>
doc.write (strength)
TypeError: must be str, not float
這是奇怪的;我已經將它定義爲一個字符串,所以我不知道爲什麼我得到這個錯誤。
像'round(newint)'這樣的代碼什麼都不做 - 或者確切地說:它將'newint'舍入並忽略結果。 – Matthias
將來,請將您的代碼減少到重現問題的[minimal example](http://stackoverflow.com/help/mcve),並確保您的問題格式正確 - 它有助於清楚代碼是什麼,什麼是代碼是輸入和什麼是輸出。 – jonrsharpe