鱼C论坛

 找回密码
 立即注册
查看: 5646|回复: 7

[已解决]如何让用户输入的名字不区分大小写

[复制链接]
发表于 2017-4-2 09:23:36 | 显示全部楼层 |阅读模式

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

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

x

小鱼老师你好,

1. 我想请教一下如何在下列的程序基础上增加一段代码实现让用户输入的名字不区分大小写,比如输入了"dg", "Dg","dG", 都能够确认输入正确?
2. 如果输入不正确,如何增加循环让用户继续输入呢?
====================================
print ('Guess who am i ?')
temp = input ('Pls type my name : ')
if temp == ("DG"):
    print ("You have just typed a name = " + temp)
    print ('you are right !')
else:
    print ('pls try again !')
print('welcome to play again !')
================================

Davis
最佳答案
2017-4-2 12:19:14
  1. #循环开始
  2. while True:
  3.     print ('Guess who am i ?')
  4.     # --------加了个方法,这个方法是把所有大写变为小写
  5.     temp = input('Pls type my name : ').lower()
  6.     # --------这个地方直接把判断条件变成小写就行了,因为上面已经把你的输入变成小写了
  7.     if temp == ("dg"):
  8.         print ("You have just typed a name = " + temp)
  9.         print ('you are right !')
  10.         #循环结束的条件满足就退出
  11.         break
  12.     else:
  13.         print ('pls try again !')
  14.         print('welcome to play again !')
复制代码


试一下
  1. Guess who am i ?
  2. Pls type my name : test
  3. pls try again !
  4. welcome to play again !
  5. Guess who am i ?
  6. Pls type my name : DG
  7. You have just typed a name = dg
  8. you are right !

  9. Process finished with exit code 0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-2 09:45:38 | 显示全部楼层
  1. while 1:
  2.     print ('Guess who am i ?')
  3.     temp = input ('Pls type my name : ')
  4.     if temp == ("DG") or temp == ('dg') or temp == ('Dg') or temp == ('dG'):
  5.         print ("You have just typed a name = " + temp)
  6.         print ('you are right !')
  7.         break
  8.     else:
  9.         print ('pls try again !')
  10.     print('welcome to play again !')
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-2 10:44:02 | 显示全部楼层
高级点的字符串比较函数可以设置是否大小写敏感。
也可以把输入的字符串与待比较的字符串(用户名)全部转为大写或小写进行比较。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-2 11:28:50 | 显示全部楼层
谢谢老师
如何用代码实现下列方法呢?

高级点的字符串比较函数可以设置是否大小写敏感。
也可以把输入的字符串与待比较的字符串(用户名)全部转为大写或小写进行比较。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-2 11:38:00 | 显示全部楼层
davis7713 发表于 2017-4-2 11:28
谢谢老师
如何用代码实现下列方法呢?

我不是老师

第一种方法需要找库,不使用运算符而是使用函数进行比较。
第二种方法或者对字符串的每个字符进行迭代,迭代时,如果是ascii码的话,先判断单个字符是否是大写或小写字符,然后ch-'a'+'A'可以将小写字符转换为大写字符,ch-'A'+'a'可以将大写字符转换为小写字符,最后将转换后的字符拼成一个新的字符串。
或者找转换整个字符串的函数。


到python官方,找python字符串相关的函数与工具。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-2 12:19:14 | 显示全部楼层    本楼为最佳答案   
  1. #循环开始
  2. while True:
  3.     print ('Guess who am i ?')
  4.     # --------加了个方法,这个方法是把所有大写变为小写
  5.     temp = input('Pls type my name : ').lower()
  6.     # --------这个地方直接把判断条件变成小写就行了,因为上面已经把你的输入变成小写了
  7.     if temp == ("dg"):
  8.         print ("You have just typed a name = " + temp)
  9.         print ('you are right !')
  10.         #循环结束的条件满足就退出
  11.         break
  12.     else:
  13.         print ('pls try again !')
  14.         print('welcome to play again !')
复制代码


试一下
  1. Guess who am i ?
  2. Pls type my name : test
  3. pls try again !
  4. welcome to play again !
  5. Guess who am i ?
  6. Pls type my name : DG
  7. You have just typed a name = dg
  8. you are right !

  9. Process finished with exit code 0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-2 20:06:04 | 显示全部楼层
谢谢你,我是初学的,你的水平绝对是我的老师了,周末愉快!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-6 16:39:57 | 显示全部楼层
我添加了一个识别输入格式的功能
  1. print ('Guess who am i ?')
  2. temp = input ('Pls type my name : ')
  3. while not temp.isalpha():
  4.     print("输入格式有误,请重新输入:")
  5.     temp = input("")                       #若输入的不是字母 需要重新输入(英文不好,见谅)
  6. while True:
  7.     if temp == ("DG") or temp == ("Dg") or temp == ("dg") or temp == ("dG"):
  8.         print ("You have just typed a name = " + temp)
  9.         print ('you are right !')
  10.         break
  11.     else:
  12.         print ('pls try again :', end = " ")
  13.         temp = input("")
  14. print('welcome to play again !')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-17 10:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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