简体   繁体   English

python骰子预测游戏

[英]Dice prediction game for python

I'm writing a python program that asks you to predict a die. 我正在编写一个python程序,要求您预测死亡。 You start with 5 euro and you earn 5 euro if you win, lose 1 euro if you're 1 off and lose 3 euro if you're more than 1 off. 您以5欧元开始,如果获胜,您将获得5欧元;如果是1欧元,则损失1欧元;如果超过1欧元,则损失3欧元。

def main():

    while True:
        saldo = 5
        y = input("[R]oll or [S]top")
        if y == "r" :
            p = input("What is your prediction?")
            from random import randint
            x = randint(1, 6)
            print(x)
            if p == x:
                saldo = saldo +5  
            elif p == int(x-1) or p == int(x+1):
                saldo = saldo -1
            else:
                saldo = saldo -3
            print ("saldo is " , saldo)
        elif y == "s" :
            saldo = saldo
            print ("eindsaldo is " , saldo)
            return
        else:
            print ("Enter r or s")

main()

The random part is working but not the saldo part and I can't figure out what is going wrong. 随机部分正在工作,但萨尔多部分没有工作,我无法弄清楚出了什么问题。 I am new at Python but got some experience on Javascript and other basic programming languages. 我是Python的新手,但在Javascript和其他基本编程语言方面有一些经验。 This looks like it should work though. 看起来应该可以。

Your saldo is initialised inside the while loop. 您的saldo在while循环内初始化。

This means every time you start at the beginning of the loop, your saldos are set to 5. 这意味着每次您从循环的开头开始,saldos设置为5。

def main()
    saldo = 5
    while True:
        y = input("[R]oll or [S]top")
        ...

Move the saldo = 5 outside the while loop. saldo = 5 while循环之外。

Take a look at the below code. 看下面的代码。 I took the liberty to make additional changes to make the whole thing more Pythonic and efficient. 我自由地进行了其他更改,以使整个过程变得更加Python化和高效。

Update2: I took the liberty to make some small adjustments (Antonvbr) Update2:我可以自由地进行一些小的调整(Antonvbr)

from random import randint  # 1

def main(saldo):            

    while True:
        y = input("[R]oll or [S]top").lower()  # 3

        if y == "r" :
            p = input("What is your prediction?") #4
            while p not in list("123456"):
                print("Enter a valid number 1-6")
                p = input("What is your prediction?")
            p = int(p) # convert to integer
            x = randint(1, 6)
            print(x)
            if p == x:
                saldo += 5  
            elif abs(p-x) == 1:
                saldo -= 1
            else:
                saldo -= 3
            print ("saldo is {}".format(saldo))  # 5

        elif y == "s" :
            print ("stop! saldo is {}".format(saldo))
            return saldo

        else:
            print ("Enter R/r or S/s") # Not case sensitivt anymore

startsaldo = 5
saldo = main(startsaldo) #2

Explanation: 说明:

  1. you do not want to import on every iteration, import functions are always made first 您不希望每次迭代都导入,导入函数总是最先执行的

  2. as mentioned by others saldo needs to be initialized once only. 正如其他人所提到的, saldo需要初始化一次。 Normally parameters like these are injected in the function and returned back when the function quits. 通常,此类参数会注入函数中,并在函数退出时返回。

  3. you are asking for "R" or "S" but comparing against "r" and "s" . 您要输入"R""S"但要与"r""s"进行比较。 In Python, case matters. 在Python中,大小写很重要。
  4. you only want inputs 1-6 to be valid list("123456") returns ["1","2...] and then we convert that to an int 您只希望输入1-6为有效列表(“ 123456”)返回[“ 1”,“ 2 ...],然后将其转换为int
  5. string formatting is a very valuable tool to learn. 字符串格式化是一个非常有价值的学习工具。

Also generally, var = var + 1 is written like var += 1 in Python. 通常,在Python中, var = var + 1就像var += 1一样编写。 Same applies with - too. 同样适用于-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM