gpt4 book ai didi

python - 我的 Python 老虎机中的循环问题和奇怪的代码

转载 作者:行者123 更新时间:2023-12-01 04:54:58 26 4
gpt4 key购买 nike

我已经阅读了所有其他老虎机帖子,并且我可以理解循环理论并获得所有其他解决方案,直到代码简单而简短。
我的阵列现在太糟糕了,我真的不明白为什么我不能玩超过一次

这里是我的整个数组,其中包括我在过去 2 天提出的其他问题的帮助。

如果这段代码很糟糕,请保持真诚和粗鲁:

import random
import time

cr = 100
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- start at ' + str(cr) + 'cr place your bet -]')
time.sleep(1)
slot = []
for row in range(9):
slot.append([None] * 9)


def print_slot(slot, empty='[ ]'):
for row in slot:
print(' '.join(empty if element is None else '{{: ^{}}}'.format(len(empty)).format(element) for element in row))

print_slot(slot)

time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)


def game_start():
cr = 100
bet = int(input('[- digit the num of coins to bet -] \n '))
if (cr > 0) and (0 < bet <= cr):
cr = 100 - bet
first_n = random.randint(0, 9)
second_n = random.randint(0, 9)
third_n = random.randint(0, 9)
fourth_n = random.randint(0, 9)
fifth_n = random.randint(0, 9)
slot[4][2] = str(first_n)
slot[4][3] = str(second_n)
slot[4][4] = str(third_n)
slot[4][5] = str(fourth_n)
slot[4][6] = str(fifth_n)

if first_n == second_n and second_n == third_n and third_n == fourth_n and fourth_n == fifth_n:
cr += bet * 100
time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print_slot(slot)
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- you won!!! now you have ' + str(cr) + 'cr -]')
bet = int(input('[- digit the num of coins to bet -] \n '))
return bet

else:
time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print_slot(slot)
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- you loose! now you have ' + str(cr) + 'cr -]')
bet = int(input('[- digit the num of coins to bet -] \n '))
return bet

elif cr < 0:
time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print_slot(slot)
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- you ran out of money 2 bet :( -]')

game_start()

最佳答案

每次下注时,您都必须-=信用,只需删除elif,我删除了一些不必要的行,添加了try/except来转换为一个 int 并捕获错误的输入以及用户退出游戏的方法。您可以在您喜欢的位置添加一两次 sleep ,但它应该按照您想要的方式运行:

def game_start():
cr = 100
while cr > 0:
bet = input('[- digit the num of coins to bet -] \n or enter q to quit')
if bet == "q": # if user wants to quit they can enter q
print("Thanks for playing, you are leaving with ${}".format(cr))
return # leaves/ends the function
try: # use try/except to cast to int to catch illegal input
bet = int(bet)
except ValueError:
print("Invalid input")
continue
if bet <= cr: # make sure we have enough credit
cr -= bet
first_n = random.randint(0, 9)
second_n = random.randint(0, 9)
third_n = random.randint(0, 9)
fourth_n = random.randint(0, 9)
fifth_n = random.randint(0, 9)
slot[4][2] = str(first_n)
slot[4][3] = str(second_n)
slot[4][4] = str(third_n)
slot[4][5] = str(fourth_n)
slot[4][6] = str(fifth_n)
else: # if we get here we tried to bet too much so use continue to go back to start of loop and ask for bet again
print("You don't have that much credit")
continue
# if we get here all is good
if len({first_n,second_n,third_n,fourth_n,fifth_n}) == 1: # sets don't have dups so if we have five that are equal we will only have one element
cr += bet * 100
time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print_slot(slot)
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- you won!!! now you have ' + str(cr) + 'cr -]')
else: # if we did not have a winning spin we will get here
print("Hard Luck, You have {} credit left".format(cr))
# this will print if we run out of credit
print('[- you ran out of money 2 bet :( -]')

cr = 100 - bet 保持将 cr - 等于 100 - 当前下注,它不会通过减少下注金额来更新 cr。

在代码中包含返还赌注将导致函数在您到达该语句时结束,无论您有多少信用,您只想在剩余 0 美元时中断/返还,或者用户想要离开。

关于python - 我的 Python 老虎机中的循环问题和奇怪的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27643275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com