大家好,我希望你能解决一个我还没想到的小问题。 这是对作业的描述,首先要了解我正在做的工作。
“在这部分作业中,你将编写一个输入两个不同的程序 字符形式为“c1-c2”,其中c1 是第一个字符和c2 是第二个 字符。您需要检查此输入的有效性。也就是说,输入应该 以字母开头,后跟短划线( - ),它应以另一个字母结尾 不敏感(这意味着“a”和“A”是相同的字符)。请注意 要从用户获得的字符必须彼此不同。在 如果用户输入无效输入,您的程序应该连续进行 提示所需的值,直到他输入有效值。“
以下是一些示例运行:
请在c1-c2表格中输入两个不同的字符:a-A
输入无效!
请输入两个不同的字符,格式为c1-c2:a-B (如果程序正确,程序应该停止。)
这是我到目前为止所尝试的内容:
Tensor
答案 0 :(得分:1)
此处您的流程几乎总是在第一次检查时终止:
def process(Z):
if Z[0] in ascii_letters:
return True
您需要嵌套您的条件,并且仅在填写所有条件时“返回”,这是一个快速示例:
def process(Z):
if Z[0] in ascii_letters:
if Z[1] == '-':
if Z[2] in ascii_letters:
return True
return False
请注意,在访问Z [2]之前,您需要检查Z的大小是否合适。您还需要添加有关同一字母的支票。因此,解决您的问题的方法是:
def process(Z):
if len(Z) == 3
and Z[0] in ascii_letters
and Z[1] == '-'
and Z[2] in ascii_letters
and Z[0].upper() != Z[2].upper():
return True
return False
答案 1 :(得分:1)
字符串具有可用于验证输入的方法。使用它们!您可以拆分' - '并验证您有2个长度为1的字符,检查它们是否是字母并比较每个字符的大写版本。
Caused by: java.lang.BootstrapMethodError: java.lang.LinkageError: loading constraint violation when overriding method "org/hibernate/tool/schema/spi/DelayedDropRegistry.registerOnCloseAction(Lorg/hibernate/tool/schema/spi/DelayedDropAction;)V" during creation of class "org/hibernate/internal/SessionFactoryImpl$$Lambda$207/000000001B931600": loader "java/lang/InternalAnonymousClassLoader@1337bd9b" of class "org/hibernate/internal/SessionFactoryImpl$$Lambda$207/000000001B931600" and loader "com/ibm/ws/classloader/CompoundClassLoader@d5b1949f" of class "org/hibernate/tool/schema/spi/DelayedDropRegistry" have different types for the method signature
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:460)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:535)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)
... 156 more
Caused by: java.lang.LinkageError: loading constraint violation when overriding method "org/hibernate/tool/schema/spi/DelayedDropRegistry.registerOnCloseAction(Lorg/hibernate/tool/schema/spi/DelayedDropAction;)V" during creation of class "org/hibernate/internal/SessionFactoryImpl$$Lambda$207/000000001B931600": loader "java/lang/InternalAnonymousClassLoader@1337bd9b" of class "org/hibernate/internal/SessionFactoryImpl$$Lambda$207/000000001B931600" and loader "com/ibm/ws/classloader/CompoundClassLoader@d5b1949f" of class "org/hibernate/tool/schema/spi/DelayedDropRegistry" have different types for the method signature
at sun.misc.Unsafe.defineAnonymousClass(Native Method)
at java.lang.invoke.InnerClassLambdaMetafactory.spinInnerClass(InnerClassLambdaMetafactory.java:339)
at java.lang.invoke.InnerClassLambdaMetafactory.buildCallSite(InnerClassLambdaMetafactory.java:206)
def process(Z):
parts = Z.split("-")
return (len(parts)==2 and len(parts[0])==1 and len(parts[1])==1
and parts[0].isalpha()
and parts[0].upper() == parts[1].upper())
适用于所有unicode字符,因此您无需担心用户是否以某种奇怪的语言输入文字。
答案 2 :(得分:0)
处理此问题的正确方法是使用regular expressions。它们是一个正则表达式库,作为标准python库的一部分。
import re
u_input = 'your string'
def validate(input):
# chars must be different
if input[0] == input[-1:]:
return False
# begin and end with upper or lower a-z with dash in middle
pattern = '[a-zA-Z]{1}-[a-zA-Z]{1}'
result = re.match(pattern, input)
# re.match returns None if no match
if not result:
return False:
else:
return True
答案 3 :(得分:0)
我尝试使其尽可能与您的代码类似,并且理解起来非常简单。我想我考虑了所有必要的条件。如果没有,请告诉我。
from string import ascii_letters
def validity_check(input_chars):
try:
# if cant be splitted raises Exception which results in restart
char_1, char_2 = input_chars.split("-")
# if characters not in alphabet restarts
chars_in_alphabet = char_1 in ascii_letters and char_2 in ascii_letters
if not chars_in_alphabet:
return False
# if characters are the same letter restarts
same_latter = char_1.lower() == char_2.lower()
if same_latter:
return False
# I'm not sure if you want to check if the first letter
# is small and the second one is capital
# if so add this block
# chars_letter_size = char_1 in ascii_letters.lower() and char_2 in ascii_letters.upper()
# if not chars_letter_size:
# return False
return True
except:
return False
while True:
ask = input("Please enter two different characters in the form c1-c2: ")
if validity_check(ask):
break
答案 4 :(得分:0)
内置功能 -isalpha()将有助于您的作业。
while True:
ask=input('Please enter two different characters in the form c1-c2: ')
if len(ask)!=3:
continue
if ask[1] == '-' and ask[0].isalpha() and ask[2].isalpha() and ask[0]!=ask[2]:
break
如果给定的字符串包含所有字母, isalpha()函数将返回 True 。