随机库的Python问题 [英] Python problem with random library

查看:86
本文介绍了随机库的Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个问题,我试图把所有可能的数字放在一边,但没有人是正确的。为什么?非常感谢你。



  import 随机
casual = random.randint( 1 10
user = 0
while user!= casual:
user = input( 类型:
如果 user == casual:
print correct
else
print 错误





我尝试过:



此外,如果我先打印随机数然后输入,那就错了。我无法理解。

解决方案

初看起来我没有看到任何明显的东西,但我不是Python专家。



使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


一个简单的调试会告诉你发生了什么:打印每个变量的值表明 user 包含字符串值。因此,您需要使用 int()函数将其转换为数字。


Hey, I have a problem, I tried to put all possible numbers but noone is the correct. Why? Thank you so much.

import random
casual=random.randint(1,10)
user=0
while user != casual:
    user=input("type: ")
    if user == casual:
        print("correct")
    else:
        print("wrong")



What I have tried:

Also if I print the random number first and then input that, it is wrong. I can't understand.

解决方案

On first look I see nothing obvious, but I am not Python specialist.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


A simple bit of debugging would show you what is happening: printing the values of each variable shows that user contains a string value. So you need to use the int() function to convert it to a number.


这篇关于随机库的Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆