鱼C论坛

 找回密码
 立即注册
查看: 1894|回复: 4

[已解决]新手求助,条件设定问题

[复制链接]
发表于 2020-9-23 22:22:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. print('|--- 欢迎进入通讯录程序 ---|')
  2. print('|--- 1:查询联系人资料 ---|')
  3. print('|--- 2:插入新的联系人 ---|')
  4. print('|--- 3:删除已有的联系人 ---|')
  5. print('|--- 4:查看所有通讯录 ---|')
  6. print('|--- 5:退出通讯录程序 ---|')


  7. contacts = dict()

  8. while True:
  9.     instr = input('\n请输入相关的指令编号:')

  10.     if instr.isdigit():
  11.         instr = int(instr)
  12.     else:
  13.         print('抱歉,您的输入有误,请重新输入!')

  14.     if instr == 1:
  15.         name = input('请输入联系人姓名:')
  16.         if name in contacts:
  17.             print('姓名\t手机号码')
  18.             print(name + '\t' + contacts[name])
  19.         else:
  20.             print('抱歉,您输入的姓名不在通讯录中!')

  21.     if instr == 2:
  22.         name = input('请输入联系人姓名:')
  23.         if name in contacts:
  24.             print('您输入的姓名在通讯录中已存在 -->> ', end='')
  25.             print(name + ': ' + contacts[name])
  26.             if input('是否修改用户资料(YES/NO):').upper() == 'YES':
  27.                 contacts[name] = input('请输入用户联系电话:')
  28.         else:
  29.             contacts[name] = input('请输入用户联系电话:')
  30.             print('保持联系人' + name + '成功!')

  31.     if instr == 3:
  32.         name = input('请输入联系人姓名:')
  33.         if name in contacts:
  34.             del(contacts[name])
  35.         else:
  36.             print('您输入的联系人不存在。')

  37.     if instr == 4:
  38.         print('姓名\t手机号码')
  39.         for key, value in contacts.items():
  40.             print(key, value)      

  41.     if instr == 5:
  42.         break

  43. print('|--- 感谢使用通讯录程序! ---|')
复制代码




我想添加一个条件,即当输入的数据不在(1,6)的范围内时,程序会提示输入错,请重新输入,但是尝试了好久都没有成功过,希望大佬帮帮忙看看怎么实现
最佳答案
2020-9-24 00:14:58
  1. print('|--- 欢迎进入通讯录程序 ---|')
  2. print('|--- 1:查询联系人资料 ---|')
  3. print('|--- 2:插入新的联系人 ---|')
  4. print('|--- 3:删除已有的联系人 ---|')
  5. print('|--- 4:查看所有通讯录 ---|')
  6. print('|--- 5:退出通讯录程序 ---|')


  7. contacts = dict()

  8. while True:
  9.     instr = input('\n请输入相关的指令编号:')
  10.     while instr not in ['1','2','3','4','5','6']: #这里加两行判断即可
  11.         instr = input('\n输入错误,请输入正确的的指令编号:')
  12.     if instr.isdigit():
  13.         instr = int(instr)
  14.     else:
  15.         print('抱歉,您的输入有误,请重新输入!')

  16.     if instr == 1:
  17.         name = input('请输入联系人姓名:')
  18.         if name in contacts:
  19.             print('姓名\t手机号码')
  20.             print(name + '\t' + contacts[name])
  21.         else:
  22.             print('抱歉,您输入的姓名不在通讯录中!')

  23.     if instr == 2:
  24.         name = input('请输入联系人姓名:')
  25.         if name in contacts:
  26.             print('您输入的姓名在通讯录中已存在 -->> ', end='')
  27.             print(name + ': ' + contacts[name])
  28.             if input('是否修改用户资料(YES/NO):').upper() == 'YES':
  29.                 contacts[name] = input('请输入用户联系电话:')
  30.         else:
  31.             contacts[name] = input('请输入用户联系电话:')
  32.             print('保持联系人' + name + '成功!')

  33.     if instr == 3:
  34.         name = input('请输入联系人姓名:')
  35.         if name in contacts:
  36.             del(contacts[name])
  37.         else:
  38.             print('您输入的联系人不存在。')

  39.     if instr == 4:
  40.         print('姓名\t手机号码')
  41.         for key, value in contacts.items():
  42.             print(key, value)      

  43.     if instr == 5:
  44.         break

  45. print('|--- 感谢使用通讯录程序! ---|')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-24 00:14:58 | 显示全部楼层    本楼为最佳答案   
  1. print('|--- 欢迎进入通讯录程序 ---|')
  2. print('|--- 1:查询联系人资料 ---|')
  3. print('|--- 2:插入新的联系人 ---|')
  4. print('|--- 3:删除已有的联系人 ---|')
  5. print('|--- 4:查看所有通讯录 ---|')
  6. print('|--- 5:退出通讯录程序 ---|')


  7. contacts = dict()

  8. while True:
  9.     instr = input('\n请输入相关的指令编号:')
  10.     while instr not in ['1','2','3','4','5','6']: #这里加两行判断即可
  11.         instr = input('\n输入错误,请输入正确的的指令编号:')
  12.     if instr.isdigit():
  13.         instr = int(instr)
  14.     else:
  15.         print('抱歉,您的输入有误,请重新输入!')

  16.     if instr == 1:
  17.         name = input('请输入联系人姓名:')
  18.         if name in contacts:
  19.             print('姓名\t手机号码')
  20.             print(name + '\t' + contacts[name])
  21.         else:
  22.             print('抱歉,您输入的姓名不在通讯录中!')

  23.     if instr == 2:
  24.         name = input('请输入联系人姓名:')
  25.         if name in contacts:
  26.             print('您输入的姓名在通讯录中已存在 -->> ', end='')
  27.             print(name + ': ' + contacts[name])
  28.             if input('是否修改用户资料(YES/NO):').upper() == 'YES':
  29.                 contacts[name] = input('请输入用户联系电话:')
  30.         else:
  31.             contacts[name] = input('请输入用户联系电话:')
  32.             print('保持联系人' + name + '成功!')

  33.     if instr == 3:
  34.         name = input('请输入联系人姓名:')
  35.         if name in contacts:
  36.             del(contacts[name])
  37.         else:
  38.             print('您输入的联系人不存在。')

  39.     if instr == 4:
  40.         print('姓名\t手机号码')
  41.         for key, value in contacts.items():
  42.             print(key, value)      

  43.     if instr == 5:
  44.         break

  45. print('|--- 感谢使用通讯录程序! ---|')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-24 00:15:39 | 显示全部楼层
第十三十四行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-24 11:24:19 | 显示全部楼层
  1. print('|--- 欢迎进入通讯录程序 ---|')
  2. print('|--- 1:查询联系人资料 ---|')
  3. print('|--- 2:插入新的联系人 ---|')
  4. print('|--- 3:删除已有的联系人 ---|')
  5. print('|--- 4:查看所有通讯录 ---|')
  6. print('|--- 5:退出通讯录程序 ---|')

  7. contacts = dict()

  8. while True:
  9.     instr = input('\n请输入相关的指令编号:')
  10.     while instr not in ['1','2','3','4','5']: #这里加两行判断即可
  11.         instr = input('\n输入错误,请输入正确的的指令编号:')
  12.     #instr = int(instr)#其实下面直接与字符比较即可,若你想与数字比较,那就在这里转一下然后把下面的if后面都改回数字
  13.     # 下面用if…elif…else…的模式可以减少很多次判断
  14.     if instr == '1':
  15.         name = input('请输入联系人姓名:')
  16.         if name in contacts:
  17.             print('姓名\t手机号码')
  18.             print(name + '\t' + contacts[name])
  19.         else:
  20.             print('抱歉,您输入的姓名不在通讯录中!')

  21.     elif instr == '2':
  22.         name = input('请输入联系人姓名:')
  23.         if name in contacts:
  24.             print('您输入的姓名在通讯录中已存在 -->> ', end='')
  25.             print(name + ': ' + contacts[name])
  26.             if input('是否修改用户资料(YES/NO):').upper() == 'YES':
  27.                 contacts[name] = input('请输入用户联系电话:')
  28.         else:
  29.             contacts[name] = input('请输入用户联系电话:')
  30.             print('保持联系人' + name + '成功!')

  31.     elif instr == '3':
  32.         name = input('请输入联系人姓名:')
  33.         if name in contacts:
  34.             del(contacts[name])
  35.         else:
  36.             print('您输入的联系人不存在。')

  37.     elif instr == '4':
  38.         print('姓名\t手机号码')
  39.         for key, value in contacts.items():
  40.             print(key, value)      

  41.     else:
  42.         break

  43. print('|--- 感谢使用通讯录程序! ---|')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-24 12:12:09 | 显示全部楼层
谢谢各位大佬帮忙
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 18:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表