Python 从输入输出打印出字母
我做了这个函数,我想把它调整成两个参数。一个代表字母,一个代表数字。函数应打印适当数量的字母。(例如,如果用户键入“monkey”和“3”,函数将输出“mon”) 到目前为止,这是我的代码Python 从输入输出打印出字母,python,python-3.x,Python,Python 3.x,我做了这个函数,我想把它调整成两个参数。一个代表字母,一个代表数字。函数应打印适当数量的字母。(例如,如果用户键入“monkey”和“3”,函数将输出“mon”) 到目前为止,这是我的代码 word = input ("What is your word?") number = input ("What is your number? ") def test(): letter=word[0] print (letter) test() 这就是你想要的吗 word = in
word = input ("What is your word?")
number = input ("What is your number? ")
def test():
letter=word[0]
print (letter)
test()
这就是你想要的吗
word = input("What is your word?")
number = int(input("What is your number? "))
def test(word, number):
print(word[:number])
test(word, number)
结果:
What is your word?monkey
What is your number? 3
mon
这不应该给你在朱利安·伯努的问题中提到的语法错误
注意:这是假设您使用的是最新版本的python我使用的是Python2,请尝试
word[:int(number)]
它会产生无效的语法。无论如何,请不要仅仅因为它使用的是与您不同的python版本而否决某个解决方案。我从来没有这样做过;P我需要至少15个声誉如果Jakus使用Python3,则不能使用原始输入。只使用输入。您也不需要在提供的单词周围添加引号。还必须将数字转换为int。
word = input ("What is your word: ")
number = input ("What is your number: ")
def test():
print(word[:int(number)])
test()