numpy 为什么这两种方法都行不通???每次都印错[重复]

cmssoen2  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(82)

此问题已在此处有答案

How can I read inputs as numbers?(10个答案)
5天前关闭。

# guessing game using real random numbers

import os
import numpy as np
import time
import random

# r = np.array(\[13, 14, 16, 89, 45, 34, 99, 32, 87, 94, 83, 51, 2, 18, 21\])
# rr = np.random.choice(r)
r = random.randint(1,99)
print("Welcome to the guessing game...")
print(r)
time.sleep(2)
i = 0 
while i == 0:
      
        a = input("pick a number between 1-100...")
      
        if a == r:
                print("correct")
                i += 1
        else:   
                print("wrong")

很明显这只是个简单的猜谜游戏。我先用numpy数组试过,然后用randint

envsm3lx

envsm3lx1#

您的用户输入将是字符串。你需要把它转换成一个数字。

a = int(input("pick a number between 1-100..."))

例如,将工作。但是,当用户键入不是数字的内容时,它会崩溃。

相关问题