python 中的 if 语句、while 循环和字典有问题
I have a problrm with if statement and while loop and dictionary in python
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif r not in men:
print('Sorry, the number is not found')
elif r >= 11:
while r >= 11:
r = r + 1 == 1
print('This is invalid number')
mo()
我在 elif
中遇到问题。我想要的是,如果我在控制台中写入超过 10 个字符,那么代码将打印 'This is invalid number'
。但是代码打印 'Sorry, the number is not found'
.
输出:
Please Enter The Number: 111111111111111
Sorry, the number is not found
Process finished with exit code 0
我不确定您为什么要在代码中使用 while
循环和 r = r + 1 == 1
。但我猜你想要这样的东西:
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif len(str(r)) >= 11:
print('This is invalid number')
elif r not in men:
print('Sorry, the number is not found')
mo()
if / elif / else
运行直到满足第一个条件。
在您的情况下,满足的第一个条件是 r not in men
if(condition):
do something
elif(condition):
do something else
elif(condition):
do something else
else:
do something
您可以 re-order 它们,以便在检查它是否在字典中之前进行长度检查。
ksohan 发布的答案是正确的,但有一个问题。你想要的是如果输入的数字大于 10 个字符,那么控制台将打印 'This is invalid number'。如果你想比较字符的长度,那么你应该首先将 r
变量类型转换为字符串,然后将其长度与 10.
进行比较
试试这个:
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif len(str(r)) > 10:
print('This is invalid number')
elif r not in men:
print('Sorry, the number is not found')
mo()
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif r not in men:
print('Sorry, the number is not found')
elif r >= 11:
while r >= 11:
r = r + 1 == 1
print('This is invalid number')
mo()
我在 elif
中遇到问题。我想要的是,如果我在控制台中写入超过 10 个字符,那么代码将打印 'This is invalid number'
。但是代码打印 'Sorry, the number is not found'
.
输出:
Please Enter The Number: 111111111111111
Sorry, the number is not found
Process finished with exit code 0
我不确定您为什么要在代码中使用 while
循环和 r = r + 1 == 1
。但我猜你想要这样的东西:
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif len(str(r)) >= 11:
print('This is invalid number')
elif r not in men:
print('Sorry, the number is not found')
mo()
if / elif / else
运行直到满足第一个条件。
在您的情况下,满足的第一个条件是 r not in men
if(condition):
do something
elif(condition):
do something else
elif(condition):
do something else
else:
do something
您可以 re-order 它们,以便在检查它是否在字典中之前进行长度检查。
ksohan 发布的答案是正确的,但有一个问题。你想要的是如果输入的数字大于 10 个字符,那么控制台将打印 'This is invalid number'。如果你想比较字符的长度,那么你应该首先将 r
变量类型转换为字符串,然后将其长度与 10.
试试这个:
men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
6666666666: 'Faisal', 7777777777: 'Layla'}
def mo():
r = int(input('Please Enter The Number: '))
if r in men:
print(men[r])
elif len(str(r)) > 10:
print('This is invalid number')
elif r not in men:
print('Sorry, the number is not found')
mo()