我的代碼並沒有完全按照我想要的方式工作。[解決了]

諾索格拉

我的代碼有問題,我非常需要一些幫助,這是我的一個非常具體的問題,所以我知道要問而不是等待其他人。我對 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的迭代器没有按照预期的方式工作

表单元格自动布局的问题没有按照我想要的方式间隔

矩形没有按照我想要的方式移动

为什么物理学没有按照我想要的方式工作?

如果其他声明不能按照我想要的方式在php中工作

Java GUI不能按照我想要的方式显示吗?

我很难尝试按照我想要的方式关闭此模式

DISTINCT不按照我想要的方式与ORDER合作

Bootstrap 响应式查询没有按照我想要的方式工作

C# UWP 按钮没有按照我想要的方式对齐

我的代碼停在中間並且沒有錯誤 c++

为什么我的代码没有按照我期望的方式工作?

我为事件监听器生成了一个构造函数。但它没有按照我想要的方式工作

regextract 在 Google Sheets 中没有按照我想要的方式工作

Github 没有完全按照本地查看的方式显示我的页面

无法在 Vue.js 中点击以按照我想要的方式工作

我的代码没有按照我想要的方式运行

將代碼從 long 轉換為 BitSet - 它運行但沒有正確解決

為什麼我的 javascript 代碼沒有按預期工作

我寫的代碼正確還是有更優化的方法來解決問題?

子查詢沒有按照我想要的方式工作

為什麼我的並行代碼比沒有並行更慢

在 R Shiny 中,isTruthy() 函數沒有按照我認為應該的方式工作

為什麼我的代碼不生成沒有 str 的隨機數?

有沒有辦法重構這段代碼並使其工作?

我的 HTML 代碼沒有選擇本地 javascript 文件

為什麼我的代碼沒有增加計數?

為什麼此代碼中沒有 server.emit 並且我在節點 js 中無法理解此代碼?

PIL图像没有按照我想要的方式裁剪