【发布时间】:2020-10-09 20:05:08
【问题描述】:
我正在尝试编写刽子手。我很新,想知道一些事情。我已经想通了
x = "Hello World"
print(x[2])
print(x[6])
print(x[10])
输出是
l
W
d
我想知道我是否可以这样写:
import random
list = ["Hi","Hello","Hi wrld","Hello world"]
chosen = list(random.randint(1,4)
nr_of_letters = len(chosen)
# j is the letter the user guesses
j = input()
if j == chosen[0,nr_of_letters]:
print("something")
else:
print("something else")
我得到的错误信息是数字必须是整数。如果我尝试以不同的方式编写它,我有两个选择。
#the first option is :
j = int(input ())
#the second option is:
if j == chosen[0,int(nr_of_letters)]:
它们都不起作用。请问我怎么写正确。
【问题讨论】:
-
试试:
chosen = list[random.randint(0,3)]
标签: python list variables random input