【问题标题】:How to have input in Python only take in string and not number or anything else only letters如何在 Python 中输入只接受字符串而不是数字或其他任何字母
【发布时间】:2022-11-25 09:59:53
【问题描述】:

我是 Python 的初学者,所以请不要使用复杂或高级的代码。

contact = {}

def display_contact():
    for name, number in sorted((k,v) for k, v in contact.items()):
        print(f'Name: {name}, Number: {number}')



#def display_contact():
# print("Name\t\tContact Number")
# for key in contact:
#    print("{}\t\t{}".format(key,contact.get(key)))

while True:
  choice = int(input(" 1. Add new contact \n 2. Search contact \n 3. Display contact\n 4. Edit contact \n 5. Delete contact \n 6. Print \n 7. Exit \n Enter "))
  
#I have already tried


  if choice == 1:
    while True:
      try:
        name = str(input("Enter the contact name "))
        if name != str:
      except ValueError:
        continue
      else:
        break

    while True:
      try:
        phone = int(input("Enter number "))
      except ValueError:
        print("Sorry you can only enter a phone number")
        continue
      else:
        break
    contact[name] = phone
    
  elif choice == 2:
    search_name = input("Enter contact name ")
    if search_name in contact:
      print(search_name, "'s contact number is ", contact[search_name])
    else: 
      print("Name is not found in contact book")
      
  elif choice == 3:
    if not contact:
      print("Empty Phonebook")
    else: 
      display_contact()
      
  elif choice == 4:
    edit_contact = input("Enter the contact to be edited ")
    if edit_contact in contact:
      phone = input("Enter number")
      contact[edit_contact]=phone
      print("Contact Updated")
      display_contact()
    else:
      print("Name is not found in contact book")
      
  elif choice == 5:
    del_contact = input("Enter the contact to be deleted ")
    if del_contact in contact:
      confirm = input("Do you want to delete this contact Yes or No? ")
      if confirm == 'Yes' or confirm == 'yes':
        contact.pop(del_contact)
      display_contact
    else:
      print("Name is not found in phone book")



  elif choice == 6:
    sort_contact = input("Enter yes to print your contact")
    if sort_contact in contact:
      confirm = input("Do you want to print your contact Yes or No? ")
      if confirm == 'Yes' or confirm == 'yes':
        strs = [display_contact]
        print(sorted(strs))     
    else:
      print("Phone book is printed.")
  else:
        break

我试过了,但一直出错,我不知道如何让它只接受字符串或字母作为输入,而不是数字。

if choice == 1:
    while True:
      try:
        name = str(input("Enter the contact name "))
        if name != str:
      except ValueError:
        continue
      else:
        break

它不工作我的代码仍然接受整数和字符串中的 ans。

我是初学者,所以我可能犯了很多错误。感谢您的耐心等待。

【问题讨论】:

  • 一个字符串可以包含数字......你想允许哪些确切的字符?
  • 只有字母 a-z,没有数字或特殊字符。

标签: python python-3.x list data-structures


【解决方案1】:

您可以使用正则表达式:

import re

while True:
    name = input("Enter the contact name ")
    if re.fullmatch(r'[a-zA-Z]+', name):
        break

【讨论】:

  • 非常感谢你就像一个魅力,再次感谢。我将不得不仔细研究它以尝试完全理解它是如何工作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
  • 2022-01-07
  • 1970-01-01
  • 2018-05-13
  • 2017-05-05
  • 2021-10-30
  • 2013-06-03
相关资源
最近更新 更多