当代码没有出现时,如何修复代码中的变量

2024-05-20 05:10:45 发布

您现在位置:Python中文网/ 问答频道 /正文

所以当我运行下面的代码时,我注意到我的变量myNamemyAge根本不起作用。我这里有什么不对劲吗

这个程序打招呼并询问我的名字

print ('Hello world!')

print ('What is your name?') # ask for their name

myName = input('Michael')

print ('It is good to meet you, ' + myName)

print('The length of your name is:')

print (len(myName))

print ('What is your age?') # ask for their age

myAge = input('16')


print('You will be " + str(int(myAge) + 1) + ' in a year.')

Tags: 代码name程序forinputageyouris
3条回答

一个基本的逻辑错误是,在输入以下内容时出现语法错误 输入语法

input("MessageYouWantToAsk")

代码应如下所示

print ('Hello world!')
myName = input('What is your name?') # Ask for the name 
print ('It is good to meet you, ' + myName) # prints the name and greets
print('The length of your name is:')
print (len(myName)) # Prints the length of that name 

myAge = input('What is your Age?')   # What is the input of the age

print('You will be ' + str(int(myAge) + 1) + ' in a year.') # prints the next year age

请记住,无论何时打开'字符串,都应该使用相同的'字符串闭包来公开它

用python编程是新手的错误

如果您键入input()意味着您需要用户的输入,我想您需要

print ('Hello world!')

print ('What is your name?') # ask for their name

myName = 'Michael'

print ('It is good to meet you, ' + myName)

print('The length of your name is:')

print (myName)

print ('What is your age?') # ask for their age

myAge = '16'

print("You will be " + str(int(myAge) + 1) + " in a year.")

请注意,变量被指定为

variable = "value"

这行“print('您将在一年内成为“+str(int(myAge)+1)+'”)”,您使用的是两种不同类型的引号“and”,一次只能使用一种类型

这是一个字符串:“迈克尔” 或者像这样:“迈克尔”

上面引号中的项目是python中的有效字符串

“Michael”在python中是无效字符串。混合了两种不同类型的引号符号

考虑运行我的代码版本,看看是否有意义。

print ('Hello world!')

myName = input('What is your name? ')  # ask for their name

print ('It is good to meet you, ' + myName) # Shows the name 

print('The length of your name is:')

print (len(myName)) # Shows the length of the name 

myAge = input('What is your age? ')  # ask for their age

print("You will be " + str(int(myAge) + 1) + " in a year.")  # Shows the age + 1

相关问题 更多 >