Python 2.7 我只想在字符串中间输入一个随机数
我只是想让我的生成器生成的怪物的肢体数量在1到100之间。我不知道为什么self.number不起作用。我在self.numbers中的print语句中不断遇到无效语法错误Python 2.7 我只想在字符串中间输入一个随机数,python-2.7,Python 2.7,我只是想让我的生成器生成的怪物的肢体数量在1到100之间。我不知道为什么self.number不起作用。我在self.numbers中的print语句中不断遇到无效语法错误 import random from random import randint class Monster(object): def __init__(self): self.names = random.choice(["Michael", "Amy", "June", "Margaret"
import random
from random import randint
class Monster(object):
def __init__(self):
self.names = random.choice(["Michael", "Amy", "June", "Margaret"])
self.appearances = random.choice(["a beautiful", "a hideous", "a transparent", "a toxic"])
self.animals = random.choice(["dog", "cat", "bird", "fish"])
self.attributes = random.choice(["that can fly.", "that speaks in a human voice.", "that twists unaturally.", "that is too hot to touch."])
self.features = random.choice(["arms", "legs", "tentacles", "heads"])
self.numbers = print random.randint(1, 100)
def __str__(self):
return ' '.join(["The Human", self.names, "is here,", self.appearances,
self.animals, self.attributes, "It has", self.numbers,
self.features,])
# Create 5 unique monsters
M1 = Monster()
M2 = Monster()
M3 = Monster()
M4 = Monster()
M5 = Monster()
# Prints the descriptions of the monsters:
print M1
print M2
print M3
print M4
print M5
input('Press ENTER to exit')
替换
self.numbers = print random.randint(1, 100)
与
print函数将打印到shell中,并且不会创建字符串。您需要的是使用函数str()将随机生成的数字转换为字符串
您的代码:
import random
from random import randint
class Monster(object):
def __init__(self):
self.names = random.choice(["Michael", "Amy", "June", "Margaret"])
self.appearances = random.choice(["a beautiful", "a hideous", "a transparent", "a toxic"])
self.animals = random.choice(["dog", "cat", "bird", "fish"])
self.attributes = random.choice(["that can fly.", "that speaks in a human voice.", "that twists unaturally.", "that is too hot to touch."])
self.features = random.choice(["arms", "legs", "tentacles", "heads"])
self.numbers = str(randint(1, 100))
def __str__(self):
return ' '.join(["The Human", self.names, "is here,", self.appearances,
self.animals, self.attributes, "It has", self.numbers,
self.features,])
# Create 5 unique monsters
M1 = Monster()
M2 = Monster()
M3 = Monster()
M4 = Monster()
M5 = Monster()
# Prints the descriptions of the monsters:
print M1
print M2
print M3
print M4
print M5
input('Press ENTER to exit')
和结果
The Human Margaret is here, a hideous fish that can fly. It has 65 arms
The Human Michael is here, a toxic fish that is too hot to touch. It has 75 heads
The Human Margaret is here, a transparent dog that can fly. It has 23 tentacles
The Human Margaret is here, a transparent dog that is too hot to touch. It has 64 arms
The Human Margaret is here, a hideous bird that twists unaturally. It has 46 heads
Press ENTER to exit
你可以用一个数字f>>>从random import randint>>>>randint(1100)72>>>str(randint(1100))生成一个字符串我要等6分钟才能回答这个问题,但是你的答案非常有效,非常感谢。如果有效,你可以接受答案。以后,添加错误描述:MyPython给出语法错误并突出显示函数print。它将让我们了解出了什么问题。此外,还应包括python版本、操作系统和其他可能有用的信息。祝你学习Python好运。进一步阅读:
The Human Margaret is here, a hideous fish that can fly. It has 65 arms
The Human Michael is here, a toxic fish that is too hot to touch. It has 75 heads
The Human Margaret is here, a transparent dog that can fly. It has 23 tentacles
The Human Margaret is here, a transparent dog that is too hot to touch. It has 64 arms
The Human Margaret is here, a hideous bird that twists unaturally. It has 46 heads
Press ENTER to exit