在我試圖在這里編寫的代碼中,我需要它不斷檢查字符串的長度是否能被 3 整除,如果是,則輸出有效輸出,如果不是,則再次循環(huán)。我事先在代碼中定義了所有其他變量,例如 a、u()。def u(): if int(len(str(a))) ('is divisible by 3'): print('The sum of all characters in that string is divisible by 3') while int(len(str(a))) ('is not divisible by 3'): print('The sum of all characters in that string is NOT divisible by 3, try again!') u()u()一個簡短而有效的答案將不勝感激,謝謝!
1 回答

catspeake
TA貢獻1111條經(jīng)驗 獲得超0個贊
在這里,我使用了來自用戶的輸入并檢查是否滿足條件。
def u():
a = input(' Input string ')
if int(len(str(a)))%3 ==0: #('is divisible by 3'):
print('The sum of all characters in that string is divisible by 3')
else: # int(len(str(a))) ('is not divisible by 3'):
print('The sum of all characters in that string is NOT divisible by 3, try again!')
u()
u()
添加回答
舉報
0/150
提交
取消