python-3.x 如何将数字添加到变量中?[closed]

ssm49v7z  于 2022-11-26  发布在  Python
关注(0)|答案(4)|浏览(152)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

2天前关闭。
这篇文章是昨天编辑并提交审查的。
Improve this question
所以,我一直在尝试构建一个python数字猜谜游戏。我是新手,我不知道如何将+1添加到我的机会变量中。我尝试过像这里这样的+=1,但无论如何,它总是显示1作为输出。我知道这段代码有很多错误,但请记住,我是编码新手。

import random

numbers = 1,2,3,4,5,6,7,8,9,10 
user = None
hidden = random.choice(numbers)

print("Welcome to volty's's number guessing game!")
def game():
  chance = 0
  user = int(input("choose a number from 1 to 10: "))
  if user > hidden:
    print ("ur number is more than the hidden number")
    game()
    chance += 1
  elif user < hidden: 
   print ("ur number is less than the hidden number")
   game()
   chance = +1
  elif user == hidden:
    print (" u guessed the hidden number!")
    print ("the hidden number was:",hidden)
    print (f"u guessed it in {chance +1} step {'s' if chance > 1 else ' '}")
  
game()

这就是代码。

42fyovps

42fyovps1#

调用函数时,必须将变量chance传递给该函数。然后,在递归调用函数时,可以直接递增chance

import random

numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
user = None
hidden = random.choice(numbers)

print("Welcome to volty's's number guessing game!")

chance = 0

def game(chance):
    user = int(input("choose a number from 1 to 10: "))
    if user > hidden:
        print("ur number is more than the hidden number")
        game(chance + 1)
        # chance += 1
    elif user < hidden:
        print("ur number is less than the hudden number")
        game(chance + 1)
        # chance += 1
    elif user == hidden:
        print(" u guessed the hidden number!")
        print("the hidden number was:", hidden)
        print(f"u guessed it in {chance + 1} step{'s' if chance > 1 else ' '}")

game(chance)
xdyibdwo

xdyibdwo2#

这里最大的问题是使用递归的方式。awnser很简单:递归前更改变量

import random

numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
user = None
hidden = random.choice(numbers)

print("Welcome to volty's's number guessing game!")

def game(chance):
    user = int(input("choose a number from 1 to 10: "))
    if user > hidden:
        print("ur number is more than the hidden number")
        chance += 1
        game(chance)
    elif user < hidden:
        print("ur number is less than the hudden number")
        chance += 1
        game(chance)
    elif user == hidden:
        print(" u guessed the hidden number!")
        print("the hidden number was:", hidden)
        print(f"u guessed it in {chance + 1} step {'s' if chance > 1 else ' '}")

game(0)

查看有关递归的教程

m1m5dgzv

m1m5dgzv3#

这是因为你不必在每次失败时都重新调用函数“game()”。理想情况下,你应该创建条件“is_hidden_found”,该条件只有在玩家成功找到数字时才为真。因此,相同的变量一直使用到赢数和“几率”递增为止。

import random

print("Welcome to volty's's number guessing game!")
def game():
  numbers = 1,2,3,4,5,6,7,8,9,10 
  user = None
  hidden = random.choice(numbers) # maybe use random.randint(0, 10)

  is_hidden_found = False
  chance = 0
  while is_hidden_found == False:

    user = int(input("choose a number from 1 to 10: "))
    if user > hidden:
      print ("ur number is more than the hidden number")
      chance += 1
    elif user < hidden: 
     print ("ur number is less than the hudden number")
     chance += 1
    elif user == hidden:
      is_hidden_found = True
      print (" u guessed the hidden number!")
      print ("the hidden number was:",hidden)
      print (f"u guessed it in {chance +1} step {'s' if chance > 1 else ' '}")

game()

在学习的同时享受乐趣;)

mqkwyuun

mqkwyuun4#

解决方法:把你的“几率= 0”转移到函数作用域之外开始。
原因:每次代码进入if语句时,它都会将机会值重新初始化为0。

相关问题