【问题标题】:Making a guessing game and when I input 100 it says i need to go higher做一个猜谜游戏,当我输入 100 时,它说我需要走得更高
【发布时间】:2019-07-27 11:17:27
【问题描述】:

所以我做了一个猜谜游戏,计算机在 1 到 100 之间选择一个随机整数,当你猜低于这个数字时,它会告诉你猜得更高,如果你猜高于这个数字,它会告诉你猜得更低。它在大多数情况下都有效,只是当我猜到 100 时它告诉我要走得更高。

现在在您开始游戏之前,计算机将向数组中添加一个随机数,您只需匹配数组中提供的数字即可。

我尝试将数组切换到一个集合,但随后我收到以下错误消息: "'dict' 对象没有属性 'add'"

这是我正在使用的代码。

此外,当尝试写入文件时,它似乎也无法正常工作

from random import *
from random import randint
import os

numbers = []
Guesses = []

os.system('CLS')
print("I want you to guess my number between 1 - 100")

f = open("Data.txt", "a")

print("Say 'yes' If You're Ready")

YN = input()



if YN == "yes":
    os.system('CLS')
    for _ in range(1):
        value = randint(1, 101)
        numbers.append(value)
        break



while True:
    Guess = input()
    Guesses.append(Guess)
    print(numbers)

    if Guess < str(value):
        print("Higher")

    elif Guess > str(value):
        print("Lower")

    elif Guess == str(value):
        os.system('CLS')
        f.write(str(Guesses))
        print("That Was Correct!")
        for x in Guesses:
            print(x)
        break

input()


【问题讨论】:

  • "100" &lt; "2" - 字符串按字典顺序排序。
  • 为什么需要for _ in range(1):?此外,您还有一个break 内部。另外,对整数进行比较...

标签: python random


【解决方案1】:
  • 您的 randint end 设置为 101(请参阅 randint() function),因此在 value = 101 并且您正确猜测代码 100 的情况下国家要走得更高。
  • 打开的文件没有关闭,这可能会导致您提到的文件处理问题。
  • 有关 int/str 比较的 cmets 显示在一个示例中,并在下面修改代码的结尾

这是您的代码稍作修改并添加了一些 cmets 以显示上述项目:

from random import randint
import os

numbers = []
Guesses = []

os.system('CLS')
print("I want you to guess my number between 1 - 100")

f = open("Data.txt", "a")
try:
    print("Say 'yes' If You're Ready")

    YN = input()

    if YN == "yes":
        os.system('CLS')
        for _ in range(1):
            value = randint(1, 101)
                # start end is set from 1 to 101 (not to 100)
            numbers.append(value)
            print('DEBUG: Single item in array: ' + str(numbers) + '\n')
                # for loop is only done once as mentioned by Austin
                # and cheating for debugging :)
            # break  # not required as mentioned by Austin

    while True:
        print('Take a guess:')
        Guess = int(input())  # directly convert input to integer
        Guesses.append(Guess)
        print(numbers)

        if Guess < value:  # comparison on integer as Austin mentioned
            print("Higher")
        elif Guess > value:
            print("Lower")
        elif Guess == value:
            os.system('CLS')
            f.write(str(Guesses))
            print("That Was Correct!")
            for x in Guesses:
                print(x)
            break

    print('Guesses: ' + str(Guesses))
finally:
    f.close()
# input()  # not required

# Amendment: showing string sorting mentioned by jonrshape
hundred = '100'
two = '2'
if hundred < two:
    print('\nWrong, 100 is not smaller than 2.')
hundred = int(hundred)
two = int(two)
if hundred > two:
    print('Correct, 100 is larger than 2.')

【讨论】:

    【解决方案2】:

    @jonrsharpe 解释说,字符串是按字典顺序排序的,这意味着它将“一百”这个词按字母顺序排列。

    【讨论】:

    • 不是关于字符串"one hundred",而是字符串"100"
    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多