尝试创建一个非常基本的脚本,该脚本将显示一组字符串中的随机字符串。但是,我需要它显示好像它是一个打印功能(即没有括号或逗号)。我尝试过使用连接并遇到错误(不可用类型:列表)
name = ("Tom")
greeting = {
["Hello", name, "How are you today?"],
["Welcome", name, "How was your day?"],
["Greetings", name, "Shall we play a game?"],
["Well hey there", name, "Whats up?"],
}
print (', '.join(greeting))
真的非常感谢任何帮助。
答案 0 :(得分:0)
您的问题是您正在greeting
dictionary
而不是list
#allows us to use randint function
from random import randint
name = ("Tom")
#change greeting from a dictionary to a list by replacing { with [
greeting = [
["Hello", name, "How are you today?"],
["Welcome", name, "How was your day?"],
["Greetings", name, "Shall we play a game?"],
["Well hey there", name, "Whats up?"],
]
#assign myGreeting as a random num between 0 and 3
myGreeting = randint(0,len(greeting)-1)
#define out output to be printed to console (its a String)
output = ""
#itterate through our random greeting word by word and
#concatinate to output variable one word at a time
for myWord in greeting[myGreeting]:
output+=myWord+" "
print (output)
。
继承我对您的代码的修复:
var j = function(){
this.name = "Joe";
var no = "23";
}
希望这有帮助! 〜枪手