程序在“计算…”之后不执行任何操作

2024-06-06 12:59:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我用来计算计算机处理能力的程序的一部分。这个特殊的部分是为了找到系统中的瓶颈。如果需要,我可以提供其余的代码。开头的数字(CPU、GPU、RAM、hds)是每个部分的分数。当执行此代码时,在“计算…”消息之后不会发生任何事情。请温柔一点,我真的是个新手

from termcolor import colored
global cpus
global gpus
global rams
global hds
global type
global typ

cpus = 3.0406
gpus = 1.9893
rams = 0.8817
hds = 1.0364
type = 2
typ = 2

def btlcalc1():
  print(colored("Calculating bottleneck...", "yellow"))
  global bgpus
  global brams
  global bhds
  global bcpus
  if type == "1": #laptop
    bgpus = gpus*3.5
    brams = rams*4
    bhds = hds*5
    bcpus = cpus*2
    btlcalc2()
  if type == "2": #desktop
    btlscore = (gpus*1.75)+(rams*4)+(hds*5)+(cpus*2)
    bgpus = gpus*1.75
    brams = rams*4
    bhds = hds*5
    bcpus = cpus
    btlcalc2()
def btlcalc2():
  if bgpus <= brams and bgpus <= bhds and bgpus <= bcpus:
    gpub = 1
    cpub = 0
    ramb = 0
    hdb = 0
    btlcalc3()
  if bcpus <= brams and bcpus <= bhds and bcpus <= bgpus:
    gpub = 0
    cpub = 1
    ramb = 0
    hdb = 0
    btlcalc3()
  if brams <= bcpus and brams <= bhds and brams <= bgpus:
    gpub = 0
    cpub = 0
    ramb = 1
    hdb = 0
    btlcalc3()
  if bhds <= brams and bhds <= bcpus and bhds <= bgpus:
    gpub = 0
    cpub = 0
    ramb = 0
    hdb = 1
    btlcalc3()
def btlcalc3():
  if gpub == 1:
    print(colored("Your GPU is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your GPU score by overclocking or replacing your GPU.", "orange"))
    thanks()
  if cpub == 1:
    print(colored("Your CPU is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your CPU score by overclocking or replacing your CPU.", "orange"))
    thanks()
  if ramb == 1:
    print(colored("Your RAM is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your RAM score by overclocking, replacing, or installing more or faster RAM.", "orange"))
    thanks()
  if hdb == 1:
    print(colored("Your boot disk is the bottleneck in your system.", "yellow"))
    if typ == 1:
      print(colored("You could improve your boot disk score by replacing your HDD with a faster HDD or an SSD, or by freeing up space.", "orange"))
      thanks()
    if typ == 2:
      print(colored("You could improve your boot disk score by freeing up space.", "orange"))
      thanks()

btlcalc1()
def thanks():
  print("Done")

编辑:我修好了。如果有人想要修改代码

def btlcalc1():
  print(colored("Calculating bottleneck...", "yellow"))
  global bgpus
  global brams
  global bhds
  global bcpus
  if type == 1: #laptop
    bgpus = gpus*3.5
    brams = rams*4
    bhds = hds*5
    bcpus = cpus*2
    print("Step 1 executed succesfully.")
    btlcalc2()
  if type == 2: #desktop
    btlscore = (gpus*1.75)+(rams*4)+(hds*5)+(cpus*2)
    bgpus = gpus*1.75
    brams = rams*4
    bhds = hds*5
    bcpus = cpus
    btlcalc2()
def btlcalc2():
  global gpub
  global cpub
  global ramb
  global hdb
  if bgpus <= brams and bgpus <= bhds and bgpus <= bcpus:
    gpub = 1
    cpub = 0
    ramb = 0
    hdb = 0
    btlcalc3()
  if bcpus <= brams and bcpus <= bhds and bcpus <= bgpus:
    gpub = 0
    cpub = 1
    ramb = 0
    hdb = 0
    btlcalc3()
  if brams <= bcpus and brams <= bhds and brams <= bgpus:
    gpub = 0
    cpub = 0
    ramb = 1
    hdb = 0
    btlcalc3()
  if bhds <= brams and bhds <= bcpus and bhds <= bgpus:
    gpub = 0
    cpub = 0
    ramb = 0
    hdb = 1
    btlcalc3()
def btlcalc3():
  if gpub == 1:
    print(colored("Your GPU is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your GPU score by overclocking or replacing your GPU.", "yellow"))
    thanks()
  if cpub == 1:
    print(colored("Your CPU is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your CPU score by overclocking or replacing your CPU.", "yellow"))
    thanks()
  if ramb == 1:
    print(colored("Your RAM is the bottleneck in your system.", "yellow"))
    print(colored("You could improve your RAM score by overclocking, replacing, or installing more or faster RAM.", "yellow"))
    thanks()
  if hdb == 1:
    print(colored("Your boot disk is the bottleneck in your system.", "yellow"))
    if typ == 1:
      print(colored("You could improve your boot disk score by replacing your HDD with a faster HDD or an SSD, or by freeing up space.", "yellow"))
      thanks()
    if typ == 2:
      print(colored("You could improve your boot disk score by freeing up space.", "yellow"))
      thanks()

Tags: orandyourbyifglobalprintcolored
1条回答
网友
1楼 · 发布于 2024-06-06 12:59:08
type = 2
...
if type == '2'

整数不等于字符串。由于没有条件求值,并且没有“else”块,因此代码结束

在条件中添加一些print调试语句,并添加一个“else”以查看在您认为条件应该是好的调试步骤时是否触发了该语句

您应该阅读有关类型转换的内容,以确保在不控制输入的情况下不会发生这种情况

相关问题 更多 >