我的代碼有問題,我非常需要一些幫助,這是我的一個非常具體的問題,所以我知道要問而不是等待其他人。我對 Python 很陌生,所以如果這是一個簡單的修復,我很抱歉浪費你的時間。讓我談談這個問題,我的代碼詢問“你想計算什麼:Abs、Opp 或 Expo”,然後你輸入其中之一,例如 Abs。它要求輸入數字,但是當您插入一個數字時,它會中斷循環並返回到開頭。我不希望這發生,我真的需要這方面的幫助。我將包括我的代碼的所有行,因為我覺得每一行都很重要。
import time
import os
import re
def checkForCriteria(checkvar, criteria):
checker = len(re.findall(criteria, checkvar))
if checker:
os.system('cls')
print("ERROR NUMBER 001: In '" + checkvar + "' there were " + str(checker) + " matches of an un-wanted characters criteria.")
input("Press [ENTER] to restart.")
else:
pass
def abs(number):
if number >= 0:
return number
elif number < 0:
return number * -1
def opp(number):
return number * -1
def expo(base, exponent):
return base ** exponent
while True:
while True:
os.system('cls')
ask = input("Would you like to calculate Abs, Opp, or Expo? ")
ask = ask.upper()
if ask == "ABS":
AbsVal = input("What number do you want to use? ")
os.system('cls')
AbsCheck = checkForCriteria(AbsVal, "[^0-9, -]")
if AbsCheck == 'None':
pass
elif not AbsCheck == 'None':
break
AbsVal = int(AbsVal)
AbsVal = abs(AbsVal)
print("Your abs value is " + str(AbsVal))
exit = input("Press [ENTER] to reset!")
if ask == "OPP":
OppVal = input("What number do you want to use? ")
os.system('cls')
OppCheck = checkForCriteria(OppVal, "[^0-9, -]")
if OppCheck == 'None':
pass
elif not OppCheck == 'None':
break
OppVal = int(OppVal)
OppVal = opp(OppVal)
print("Your opp value is " + str(OppVal))
exit = input("Press [ENTER] to reset!")
if ask == "EXPO":
os.system('cls')
ExpoVal1 = input("What base number do you want to use? ")
Expo1Check = checkForCriteria(ExpoVal1, "[^0-9, -]")
if Expo1Check == 'None':
pass
elif not Expo1Check == 'None':
break
ExpoVal1 = int(ExpoVal1)
ExpoVal2 = input("What exponent number do you want to use? ")
Expo2Check = checkForCriteria(ExpoVal2, "[^0-9, -]")
if Expo2Check == 'None':
pass
elif not Expo2Check == 'None':
break
os.system('cls')
ExpoVal2 = int(ExpoVal2)
ExpoOut = expo(ExpoVal1, ExpoVal2)
print("Your exponent value is " + str(ExpoOut))
exit = input("Press [ENTER] to reset!")
這是您的代碼的一個版本,重做以使其正常工作。您的正則表達式匹配逗號和空格。我假設逗號是您所在地區的小數點,但是由於您要轉換為“int”,因此無論如何您都不需要小數,也不允許使用空格。
你應該看看我做出的與你不同的決定,以了解你做錯了什麼。
import os
import re
def checkForCriteria(checkvar, criteria):
checker = len(re.findall(criteria, checkvar))
if checker:
print("ERROR NUMBER 001: In '" + checkvar + "' there were " + str(checker) + " matches of an un-wanted characters criteria.")
return None
return 1
def abs(number):
if number >= 0:
return number
return -number
def opp(number):
return -number
def expo(base, exponent):
return base ** exponent
while True:
ask = input("Would you like to calculate Abs, Opp, or Expo? ")
ask = ask.upper()
if ask == "ABS":
AbsVal = input("What number do you want to use? ")
AbsCheck = checkForCriteria(AbsVal, "[^0-9.-]")
if not AbsCheck:
continue
AbsVal = int(AbsVal)
AbsVal = abs(AbsVal)
print("Your abs value is " + str(AbsVal))
elif ask == "OPP":
OppVal = input("What number do you want to use? ")
OppCheck = checkForCriteria(OppVal, "[^0-9.-]")
if not OppCheck:
continue
OppVal = int(OppVal)
OppVal = opp(OppVal)
print("Your opp value is " + str(OppVal))
elif ask == "EXPO":
ExpoVal1 = input("What base number do you want to use? ")
Expo1Check = checkForCriteria(ExpoVal1, "[^0-9.-]")
if not Expo1Check:
continue
ExpoVal1 = int(ExpoVal1)
ExpoVal2 = input("What exponent number do you want to use? ")
Expo2Check = checkForCriteria(ExpoVal2, "[^0-9.-]")
if not Expo2Check:
continue
ExpoVal2 = int(ExpoVal2)
ExpoOut = expo(ExpoVal1, ExpoVal2)
print("Your exponent value is " + str(ExpoOut))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句