如何修复if语句? [英] How do I fix my if statement?

查看:73
本文介绍了如何修复if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在学校的项目上遇到了一些问题。我必须使用肺算法来创建一个程序来验证信用卡。底部的if语句将不会运行并在结尾处打印消息。任何帮助将不胜感激。





 Card_Number =  int (输入( 请输入您的16位数信用卡号:)) 
总计= 0
Card_Number_String = str(Card_Number)
a = int (Card_Number_String [ 0 ])
b = int (Card_Number_String [ 1 ])
c = int (Card_Number_String [ 2 ] )
d = int (Card_Number_String [ 3 ])
e = int (Card_Number_String [ 4 ])
d = int (Card_Number_String [ 5 ])
f = int (Card_Number_String [ 6 ])
g = int (Card_Number_String [ 7 ])
h = int (Card_Number_String [ 8 ])
i = int (Card_Number_String [ 9 ])
j = int (Card_Number_String [ 10 ])
k = int (Card_Number_String [ 11 ])
l = int (Card_Number_String [ 12 ])
m = int (Card_Number_String [ 13 ])
n = int (Card_Number_String [ 14 ])
o = < span class =code-keyword> int (Card_Number_String [ 15 ])

if (a)% 2 == 0
a_Doubled = (a)* 2
if (a)> 10
Two_Digit_Number_One =(a)* 2
OneDigit1 = str(Two_Digit_Number [ 0 ])
OneDigit2 = str(Two_Digit_Number [ 1 ])
Two_Digit_Number_One_Sum = str((One_Digit1)+(One_Digit2))
print(Two_Digit_Number_One_Sum)





< b>我尝试了什么:



我已经尝试过编辑我能想到的if语句。

解决方案

if w如果16位数字以偶数开头,则工作不好

例如:4234567891111110


此测试:

  if (a)>  10 



永远不会,因为a是一位数。

你打算用 a_Doubled 吗?



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



建议:拿一张纸并尝试手工完成,您的程序应该使用相同的程序。


Hi there I'm having some problems with a project for school. I have to use the lung algorithm to create a program to validate a credit card. The if statement at the bottom will not run and print the message at the end. Any help would be greatly appreciated.


Card_Number = int ( input ( "Please enter your 16 digit credit card number:" ) )  
Total = 0
Card_Number_String = str ( Card_Number )
a = int ( Card_Number_String [ 0 ] )
b = int ( Card_Number_String [ 1 ] )
c = int ( Card_Number_String [ 2 ] )
d = int ( Card_Number_String [ 3 ] )
e = int ( Card_Number_String [ 4 ] )
d = int ( Card_Number_String [ 5 ] )
f = int ( Card_Number_String [ 6 ] )
g = int ( Card_Number_String [ 7 ] )
h = int ( Card_Number_String [ 8 ] )
i = int ( Card_Number_String [ 9 ] )
j = int ( Card_Number_String [ 10 ] )
k = int ( Card_Number_String [ 11 ] )
l = int ( Card_Number_String [ 12 ] )
m = int ( Card_Number_String [ 13 ] )
n = int ( Card_Number_String [ 14 ] )
o = int ( Card_Number_String [ 15 ] )

if ( a ) % 2 == 0:
    a_Doubled = ( a ) * 2
    if ( a ) > 10:
        Two_Digit_Number_One = ( a ) * 2 
        OneDigit1 = str ( Two_Digit_Number [ 0 ] )
        OneDigit2 = str ( Two_Digit_Number [ 1 ] )
        Two_Digit_Number_One_Sum = str ( ( One_Digit1 ) + ( One_Digit2 ) )
        print ( Two_Digit_Number_One_Sum ) 



What I have tried:

I've tried editing the if statement anyway I can think of.

解决方案

The if will work if the 16 digit number starts with an even number
eg:4234567891111110


This test :

if ( a ) > 10:


will never do because a is a single digit.
Did you intend to use a_Doubled instead ?

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
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.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.


这篇关于如何修复if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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