Python 错误 - 名称中的字母数 [英] Python Mistake - Number of letters in name

查看:22
本文介绍了Python 错误 - 名称中的字母数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来检查一个名字的长度.该程序应将名称作为用户的输入.

Write a program that checks how long a name is. The program should take a name as input from the user.

如果名字有 3 个或更少的字母,你的程序应该是这样的:

If the name has 3 or fewer letters, your program should work like this:

输入您的姓名:林林,你的名字很短.

如果名称包含 4 到 8 个字母(含),您的程序应该像这样工作:

If the name has between 4 and 8 letters (inclusive), your program should work like this:

输入您的姓名:吉米吉米,很高兴认识你.

否则,如果名称超过 8 个字母,您的程序应该是这样的:

Otherwise, if the name has more than 8 letters, your program should work like this:

请输入您的姓名:Yaasmeena嗨 Yaasmeena,你的名字很长.

这是我的尝试,但无论长度如何,它总是返回嗨 XXXXXXX,你有一个短名称".

Here's my attempt but it always returns "Hi XXXXXXX, you have a short name" regardless of the length.

Name = input('Enter your name: ')

if Name.count('Name') >= int(3):
  print ('Hi', 'Name', ',', 'nice to meet you.') 

elif Name.count('Name') <= int(3):
  print ('Hi', 'Name', ',', 'you have a short name.')

elif Name.count('Name') > int(8):
  print ('Hi', 'Name', ',', 'you have a long name.')

推荐答案

你应该使用 len(name) 而你不需要 int(3) 作为 3 已经是一个整数.您的支票应如下所示:

You should use len(name) and you don't need int(3) as 3 is already an integer. Your check should look like this:

name = input('Enter your name: ')

if len(name) >= 3:
   # do stuff

我将 Name 更改为 name 因为这是 Python 中变量命名的标准约定.

I changed Name to name as this is the standard convention of variable naming in Python.

这篇关于Python 错误 - 名称中的字母数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆