没有错误,但是运行时会显示白屏

时间:2019-04-10 12:31:16

标签: python

我使用trinket.io编写了相当大的代码。后来我开了一家商店,然后是保存功能。一次,我做了一个保存功能,代码停止工作。

我尝试修复缩进,修复拼写错误,将代码重新放入 python程序。

def main():
  reward = 0
  patt = 20 
  pdef = 10
  php = 35
  pgold= 0
  pgold = pgold + reward
  name = input("what is your name")
  print('''You are ''' +name+ ''' and you're trying to escape from the zoo but OUTTA NOWHERE A...''')
  save()

  def save():
    import random
    import time

    name = input("what is your name")
    print('''You are ''' +name+ ''' and you're trying to escape from the zoo but OUTTA NOWHERE A...''')

    number1 = random.randint(1,7)
    if number1 == 1:
        spawn = ' Monkey God'
        satt = 1000
        sdef = 9999
        shp = 1000000

    elif number1 == 2:
        spawn = ' Howard the alien'
        satt = 0
        sdef = 1
        shp = 1

    elif number1 == 3:
        spawn = ' Evan'
        satt = random.randint(4,21)
        sdef = random.randint(21,99)
        shp = random.randint(1,50)

    elif number1 == 4:
        spawn = ' Game Katana'
        satt = random.randint(1,500)
        sdef = random.randint(4,25)
        shp = random.randint(5,25)

    elif number1 == 5:
        spawn = ' John Cena'
        satt = 50
        sdef = random.randint(1,42)
        shp = 25

    elif number1 == 6:
        spawn = ' Willy Wonka'
        satt = random.randint(1,50)
        sdef = random.randint(1,50)
        shp = random.randint(1,50)
    else:
        spawn = ' Jax'
        satt = random.randint(1,25)
        sdef = random.randint(1,25)
        shp = random.randint(1,25)

    number2 = random.randint(1,4)
    if number2 == 1:
        adj = 'cringey'
        satt = satt -25

    elif number2 == 2:
        adj = 'OP'
        sdef = sdef +40

    elif number2 == 3:
        adj = 'demonic'
        shp = shp +30

    else:
        adj = 'angelic'
        sdef = sdef +3
    print ('random ' +adj + spawn+ ' appeared!')

    while php >0 and shp >0:
        choice = ('z')
        while choice != ('a'):
            print ('What u gonna do')
            print ('    a) Fight you strong man or woman')
            print ('    b) Run away because you are soooo scared that you do not want to fight')
            print ('    c) Look at dem stats booooiiii')
            choice = input()
            if choice == ('a'):
                print('You attack like a man (or woman)')
            if choice == ('b'):
                print ('you run away')
                print ('nevermind the ' +adj + spawn+ ' eats like you like a very hungry guy at McDonalds ' )
                php =0
                break
            if choice == ('c'):
                print('your own stats boooiiii')
                print('Attack = ' +str(patt)+ '   Defense = ' +str(pdef)+ '  Health = ' +str(php)+ '  Gold = ' +str(pgold) )
                print(adj + spawn+ '''\'s stats''')
                print('Attack = ' +str(satt)+ '   Defense = ' +str(sdef)+ '   Health = ' +str(shp) )
            if choice ==('a'):
                pattroll = random.randint(1,50)
                if pattroll >= sdef:
                    damage = random.randint(1,50)
                    print('You attack the  ' +adj + spawn+ ' and according to my calculations you did ' +str(damage)+ '!')
                    shp = shp - damage
                if pattroll < sdef:
                  print('you miss')
                  if shp >0:
                    sattroll = random.randint(1,50)
                    if sattroll >= pdef:
                        damage = random.randint (1,50)
                        print(' the' +adj + spawn+ ' hits you with ' +str(damage)+ '!')
                        php = php - damage
                    if sattroll < pdef:
                        print('You avoid being hit')
            if php <=0:
               print('You have died from ' +adj + spawn+ ' but, its a game so you will respawn if you click Run at the top of the screen')
            if shp <=0:
                reward = random.randint(30, 400)
                print('You win!!! you find ' +str(reward)+ ' Pieces of gold')
                pgold = pgold + reward
                php = 20
                print('You go to the hospital to heal')

    if php > 0:
      stock={'shield':1,
      'godly sword':1,
      'health pack':1,
      'wood sword':1,}
      prices={'shield': 10,
      'godly sword':4050,
      'health pack':1.5,
      'wood sword':3}

      def uppercase(x):
        return x[0].upper()+x[1:]


      print('Hi, %s, welcome to my store. Here is the menu:'%(name))
      print()

      def menu():
        for supplies in prices:
          print(uppercase(supplies))
          print('Price: $%s'%(prices[supplies]))
          print('Stock: %s'%(stock[supplies]))
        print('You have: $%s'%(pgold))

      def ask_supplies(pgold):
        supplies=input('''What do you want?
      ''')
        if supplies == ('shield'):
          php + 20
          print('your hp is now {php}')
        elif supplies == ('godly sword'):
          patt + 50
          print('your attack is now {patt}')
        elif supplies == ('health pack'):
          php + 5
          print('your hp is now {php}')
        else:
          patt + 10
          print('your attack is now {patt}')
        print()
        if supplies in stock:
          if stock[supplies]>0:
            ask_amount(supplies,pgold)
          else:
            print('''Sorry, %ss are out of stock
      '''%(supplies))
            ask_supplies(pgold)
        else:
          print('''Sorry, we don\'t have that, look at the menu.
          ''')
          ask_supplies(pgold)

      def ask_amount(supplies,pgold):
        amount=int(input('''How many %ss do you want?
      '''%(supplies)))
        print()
        if amount<=0:
          print('''At least buy one.
        ''')
          ask_amount(supplies,pgold)
        elif stock[supplies]>=amount:
          sell(supplies,amount,pgold)
        else:
          print('''Sorry, we don\'t have that many %ss.
          '''%(supplies))
          ask_amount(supplies,pgold)

      def sell(supplies,amount,pgold):
        cost=prices[supplies]*amount
        confirmation=input('''Are you sure? That will be $%s.
      -Yes
      -No
      '''%(cost)).lower()
        print()
        if confirmation=='yes':
          pgold-=cost
          print('''Thank you for shopping at Publix
      ''') 
          stock[supplies]=stock[supplies]-amount
          ask_again()
        elif confirmation=='no':
          ask_supplies()
        else:
          print('''Answer me.
      ''')
          sell(supplies,amount,pgold)

      def ask_again():
        answer=input('''Do you want anything else?
      -Yes
      -No
      ''').lower()
        print()
        if answer=='yes':
          menu()
          ask_supplies(pgold)
        elif answer=='no':
          print('Okay, bye.')
        else:
          print('Answer me.')
          ask_again()

      menu()
      ask_supplies(pgold)
      asksave = input('do you want to save and start again (Y/N)')
      if asksave == 'yes' or 'y':
        save()
      elif asksave == 'no' or 'n':
        restart=input("Do you want to play again?").lower()
        if restart == "yes" or "y":
          pgold = pgold + reward
          time.sleep(.3)
          main()

        else:
          exit()

我希望代码能够运行并显示某些内容,即使它出现故障也可以解决,但是,我想工作。(同样,我在编码方面确实非常糟糕。因此请记住这一点。)

2 个答案:

答案 0 :(得分:1)

除了其他问题/缩进,您没有调用任何函数。
您可以将以下代码放在文件末尾,看看是否可行(或显示任何错误):

if __name__ == "__main__":
    main()

答案 1 :(得分:1)

考虑到您已经正确缩进了代码,没有复制的功能名称,您根本不会调用任何函数,这就是您的结束脚本的外观

def main():
    # your  code here

if __name__ == "__main__":
    main()