为什么这个if语句不起作用 [英] Why is this if statement no working

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

问题描述

我正在尝试建立一个预订系统,但这个if语句运作得很好,



i需要你的帮助..



如果输入选择等于travel_sites,它应该执行它下面的代码。但是python正在执行else语句而不是thaat ...



  print  喜欢旅行的人们的平台......

travel_sites = {
Kashmir to Jammu:[ 18 4 ],
查谟到克什米尔:[ 18 5 ] ,
克什米尔到圣弗朗西斯科:[ 15 1 ],
克什米尔到冰岛:[ 18 2 ]
}

choic e =输入( 输入您想去的网站以及您想去的地方? =)。title()

如果选择 travel_sites:
age = input( 输入你,r age
如果 age< = choice [ 1 ]:
print (f 好吧,我们预订了你,来自{choice}的票证

else
print 好吧,我们不要



As如果输入选择等于travel_sites它应该执行它下面的代码。但是python不是执行else语句...



什么我试过了:



我试图用逗号写输入但是确实如此,没有用。

决方案
选择在travel_sites ,因为你将输入首字母大写,而travel_sites的元素不是标题案件本身永远不会为真。 .title()将可能的输入转换为 Kashmir To Jammu 而travel_sites中的内容是克什米尔到Jammu ,用而不是


< blockquote>

Quote:

为什么这个if语句没有工作



你的代码行为不顺利你期望,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

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

调试器中没有魔法,它不知道是什么你的代码应该做,它没有发现错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



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


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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



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

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

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



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


I was trying to build a booking system but this if statement is working quite well,

i require your assistance..

As if the input choice is equal to travel_sites it should execute the code below it. But instead of thaat the python is executing the else statement...

print("A Platfrom for the people who love travelling...")

travel_sites = {
     "Kashmir to Jammu" : [18,4],
    "Jammu to Kashmir" : [18,5],
    "Kashmir to San Fransisco" : [15,1],
    "Kashmir to Iceland" : [18,2]
}

choice = input("Enter the site you wanna to go and from where you wanna go ? = ").title()

if choice in travel_sites:
	age = input("Enter you,r age")
	if age <= choice[1]:
		print(f"Well, we booked you,r ticket from {choice}")

else:
	print("Well we don,t have that")


As if the input choice is equal to travel_sites it should execute the code below it. But instead of thaat the python is executing the else statement...

What I have tried:

I tried to to write the input in commas but it did,nt worked.

解决方案

choice in travel_sites will never be true because you transform the input to Title Case while the elements of travel_sites are not title case itself. .title() transforms a possible input to Kashmir To Jammu whereas what's in travel_sites is Kashmir to Jammu with to instead of To.


Quote:

Why is this if statement no working


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

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 only show you what your code is doing and your task is to compare with what it should do.


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

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