为什么2个变量的比较不起作用? [英] How come the comparing of 2 variables does not work?

查看:95
本文介绍了为什么2个变量的比较不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if语句如下:如果comp ==猜猜:

打印你赢了!

不起作用。只显示消息,您的号码大于计算机。即使他们是平等的。请解释我做错了什么。这是我的代码。



我尝试过:



import随机

打印欢迎来到我的猜谜游戏

comp =(random.randint(1,10))

def guess():

Guess = raw_input(选择1-10之间的数字)

如果猜猜==1或猜猜==2或猜猜==3 或猜猜==4或猜猜==5或猜猜==6或猜猜==7或猜猜==8或猜猜==9或猜猜==10 :

如果comp ==猜猜:

打印你赢了!

elif comp<猜猜:

打印你的号码大于电脑

elif comp>猜猜:

打印你的号码比电脑少

否则:

打印抱歉没有选择的选择

for _ in range(3):

guess()

print计算机编号为%s%comp

the if statements were it says: if comp == Guess:
print "you win!"
does not work. only message tat appears is, "your number is greater than the computers". even if they are equal. please explain what im doing wrong. here is my code.

What I have tried:

import random
print "welcome to my guessing game"
comp = (random.randint(1,10))
def guess():
Guess = raw_input("pick a number between 1-10")
if Guess == "1" or Guess == "2" or Guess == "3" or Guess == "4" or Guess == "5" or Guess == "6" or Guess == "7" or Guess == "8" or Guess == "9" or Guess == "10":
if comp == Guess:
print " You win!!"
elif comp < Guess:
print "Your number is greater than the computers"
elif comp > Guess:
print "Your number is less than the computer"
else:
print "sorry no choices were picked"
for _ in range(3):
guess()
print "the computers number was %s" % comp

推荐答案

你的问题是 comp 整数猜测字符串

您需要将输入转换为整数才能比较两个变量。



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么这样做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。

Debugger - 维基百科,免费的百科全书 [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]

调试器在这里显示你的内容代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Your problem is that comp is an integer and guess is a string.
You need to convert the input to integer in order to compare both variables.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
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.


这篇关于为什么2个变量的比较不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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