水果机GSCE挑战赛

2024-09-27 09:29:09 发布

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

我相信你们都听说过GCSE fruit machine challenge。嗯,我有问题,你看,当用户旋转3个头骨时,它不会扣除他们所有的积分,当他们只旋转2个头骨时,它不会扣除1个积分。如果有人能帮忙,请帮忙

credit = 1
import time
t = 1

while True:
         import random
         symbols = 'Star' , 'Skull'

         spin = random.choices(symbols,k=1)
         spin2 = random.choices(symbols,k=1)
         spin3 = random.choices(symbols,k=1)
         ask = input('do you want to spin? ')
         if ask == 'yes':
                  credit = (credit - 0.2)
                  credit = (round(credit, 2))
                  print('You now have... ' +str(credit) + ' credit(s).')
                  time.sleep (t)
                  print('** NOW ROLLING **')
                  time.sleep (t)
                  print('You rolled... ' +str(spin) +str(spin2) +str(spin3))
                  time.sleep (t)
                  if (spin == spin2 == 'Skull' or spin == spin3 == 'Skull' or spin2 == spin3 == 'Skull'):
                           credit = (credit - 1)
                           credit = (round(credit, 2))
                           print('Uh Oh! you rolled 2 skulls.... you lost 1 credit sorry!')
                           print('You now have a total balance of... ' +str(credit)+ ' credits!')
                           if credit >= 0.2:
                                    continue
                           else:
                                    print('Sorry! you dont have enough credits.')
                                    break
                  elif spin == 'Skull' and spin2 == 'Skull' and spin3 == 'Skull':
                           credit = (credit - credit)
                           print('You rolled 3 Skulls!! You lost all your credits!')
                           break
                  elif spin == spin2 and spin2 == spin3:
                           credit = (credit + 1)
                           print('You won 1 credit!')
                           print('You now have a total balance of... ' +str(credit)+ ' credits!')
                           if credit >= 0.2:
                                    continue
                           else:
                                    print('Sorry! you dont have enough credits.')
                                    break
                  elif spin == spin2 or spin == spin3 or spin2 == spin3:
                           credit = (credit + 0.5)
                           credit = (round(credit, 2))
                           print('You won 0.5 credits!')
                           print('You now have a total balance of... ' +str(credit)+ ' credits!')
                           if credit >= 0.2:
                                    continue
                           else:
                                    print('Sorry! you dont have enough credits.')
                                    break

                  else:
                           print('Sorry you didnt win anything.')
                           if credit >= 0.2:
                                    continue
                           else:
                                    print('Sorry! you dont have enough credits.')
                                    break
         elif ask == 'no':
                  print('Your total winnings are.... ' +str(credit))
                  break
         else:
                  print('please say yes or no..')
                  continue

Tags: oryouifhaveelsecreditsprintcredit
1条回答
网友
1楼 · 发布于 2024-09-27 09:29:09

问题是您正在将liststring进行比较,其中“Skull”是一个字符串,变量“spin”是一个元素列表。要解决这个问题,您可以使用spin = random.choice(symbols)将“spin”转换为字符串,这将作为字符串进行选择

你似乎对python不熟悉,所以我也重写了你的代码。非常欢迎您就此提出问题:)

import time
import random

t = 1
credit = 1.0

while True:
    symbols = "Star", "Skull"
    spins = random.choices(symbols, k=3)

    ask = input("Do you want to spin? ")
    if ask == "yes":
        credit -= 0.2

        print(f"You now have... {credit} credit(s).")
        time.sleep(t)
        print("** NOW ROLLING **")
        time.sleep(t)
        print("You rolled... " + " ".join(spins))
        time.sleep(t)
        if sum(spin == "Skull" for spin in spins) == 2:
            credit -= 1
            print("Uh Oh! you rolled 2 skulls.... you lost 1 credit, sorry!")
        elif sum([spin == "Skull" for spin in spins]) == 3:
            credit = 0
            print("You rolled 3 Skulls!! You lost all your credits!")
        elif all(spin == spins[0] for spin in spins):
            credit += 1
            print("You won 1 credit!")
        elif len(set(spins)) != len(spins):
            credit += 0.5
            print("You won 0.5 credits!")
        else:
            print("Sorry you didn't win anything.")

        credit = (round(credit, 2))
        print(f"You now have a total balance of... {credit} credits!")
        if credit >= 0.2:
            continue
        else:
            print("Sorry! You don't have enough credits.")
            break
    elif ask == "no":
        print(f"Your total winnings are.... {credit}")
        break
    else:
        print("Please say yes or no..")
        continue

祝你好运

相关问题 更多 >

    热门问题