|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- x = 7
- i = 1
- flag = 0
- while i <= 200:
- if (x % 2 == 1) and (x % 3 == 2 ) and (x % 5 == 4) and (x % 6 == 5) and (x % 7 == 0):
- flag = 1
- i += 1
- else:
- x = (i + 1) * 7
- i += 1
- if flag == 1:
- print('该阶梯的台阶是:',x)
- else:
- print('该题目无解')
复制代码
为啥我把“i”换成“x”就print不出来了
- x = 7
- i = 1
- flag = 0
- while x <= 200:
- if (x % 2 == 1) and (x % 3 == 2 ) and (x % 5 == 4) and (x % 6 == 5) and (x % 7 == 0):
- flag = 1
- i += 1
- else:
- x = (i + 1) * 7
- i += 1
- if flag == 1:
- print('该阶梯的台阶是:',x)
- else:
- print('该题目无解')
复制代码
问题出在你将“i”换成了“x”,导致变量名混淆。在while循环中,程序需要用到变量“i”来控制循环次数,而你将其改成了“x”,导致循环次数无法控制,从而导致程序出错。因此,你需要将“i”改回来,即将代码修改为:
- x = 7
- i = 1
- flag = 0
- while i <= 200:
- if (x % 2 == 1) and (x % 3 == 2 ) and (x % 5 == 4) and (x % 6 == 5) and (x % 7 == 0):
- flag = 1
- i += 1
- else:
- x = (i + 1) * 7
- i += 1
- if flag == 1:
- print('该阶梯的台阶是:',x)
- else:
- print('该题目无解')
复制代码
|
|