Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 高/低卡号猜谜游戏_Python - Fatal编程技术网

Python 高/低卡号猜谜游戏

Python 高/低卡号猜谜游戏,python,Python,我的导师给我们布置了一项任务:用随机整数创建一个高/低数字猜测游戏。我们必须询问用户要生成的最大值,以及我们要玩多少值 例如: 最大值:12 数值数目:6 3高(H)低(L):高 7高(H)低(L):L 9高(H)低(L):L 12高(H)低(L):L 10 Hi(H)Lo(L):L 4. 你的分数是:4 我尝试的代码: import random print("This program plays out HI/LO game with random integer values

我的导师给我们布置了一项任务:用随机整数创建一个高/低数字猜测游戏。我们必须询问用户要生成的最大值,以及我们要玩多少值

例如:

最大值:12
数值数目:6
3高(H)低(L):高
7高(H)低(L):L
9高(H)低(L):L
12高(H)低(L):L
10 Hi(H)Lo(L):L
4.
你的分数是:4
我尝试的代码:

import random

print("This program plays out HI/LO game with random integer values")

mx_val = int(input("Enter maximun value: "))
num_val = int(input("Enter how many values to play(less than max value): "))

print("Get ready to play the game")

a = []

while len(a) != num_val:
    r = random.randint(1, mx_val)
    if r not in a:
        a.append(r)

score = 0

for i in a:
    print(a[0], end=" ")
    guess = input("Enter Hi(H)or Lo(L): ")
    while guess != "H" and guess != "L":
        guess = input("You must enter 'H' or 'L': ")

    if guess == "H" and a[1] > a[0]:
        score += 1

    if guess == "L" and a[1] < a[0]:
        score += 1

    a.pop(0)

print("Final score is ", score)
随机导入
打印(“此程序使用随机整数值播放HI/LO游戏”)
mx_val=int(输入(“输入最大值:”)
num_val=int(输入(“输入要播放的值的数量(小于最大值):”)
打印(“准备好玩游戏”)
a=[]
而len(a)!=数值:
r=random.randint(1,mx_val)
如果r不在a中:
a、 附加(r)
分数=0
对于我来说,在一个:
打印(a[0],end=”“)
猜测=输入(“输入Hi(H)或Lo(L):”)
猜猜看!=“H”和猜猜看!=“L”:
猜测=输入(“您必须输入'H'或'L':”)
如果guess==“H”和a[1]>a[0]:
分数+=1
如果guess==“L”和a[1]
这是我的代码,但它没有提出适当数量的问题。它总是很短。

  • 不要迭代列表,同时从列表中删除项目
  • 您的
    列表中没有足够的值。你需要增加一倍
代码:


你可以看看这里。问题必须针对代码中的问题而不是功能。谢谢您的帮助。但我也需要它来打印最终值,而不需要输入Hi-Lo。例如11输入高(H)或低(L):L。3.
import random

print("This program plays out HI/LO game with random integer values")

mx_val = int(input("Enter maximun value: "))
num_val = int(input("Enter how many values to play(less than max value): "))

print("Get ready to play the game")

a = []

while len(a) != num_val+1:
    r = random.randint(1, mx_val)
    if r not in a:
        a.append(r)

score = 0

for i in range(num_val):
    print(a[0], end=" ")
    guess = input("Enter Hi(H)or Lo(L): ")
    while guess != "H" and guess != "L":
        guess = input("You must enter 'H' or 'L': ")

    if guess == "H" and a[1] > a[0]:
        score += 1

    if guess == "L" and a[1] < a[0]:
        score += 1

    a.pop(0)

print("Final score is ", score)
This program plays out HI/LO game with random integer values
Enter maximun value: 12
Enter how many values to play(less than max value): 6
Get ready to play the game
10 Enter Hi(H)or Lo(L): H
4 Enter Hi(H)or Lo(L): L
8 Enter Hi(H)or Lo(L): H
7 Enter Hi(H)or Lo(L): L
9 Enter Hi(H)or Lo(L): H
11 Enter Hi(H)or Lo(L): L
Final score is  2