我试图编写一个能够连接两个单词并将两个 float 相乘的函数,但我遇到了 try-error 模块的问题。我希望该函数还检查 str 是否仅由数字组成,但这不起作用。你能找到问题所在吗?
def funz():
w1=input('insert a word')
w2=input('insert a second word')
n1=input('insert a num')
n2=input('insert a second num')
try:
float(w2), float(w1)
print('I want a word')
funz()
except ValueError:
try:
float(n2), float(n1)
print('\n')
print(w1+w2)
print('\n')
print(float(n1)*float(n2))
except ValueError:
print('I said a number!')
funz()
funz()
最佳答案
isalpha 将为您提供帮助:
if w1.isalpha() and w2.isapha():
# w1 and w2 have not any numeric values
关于python - 识别仅由数字组成的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53806319/