python『学习之路01』循环语句任性玩

python 循环语句;

一 循环的基本用法及格式:.

while 循环:  --- >> 当...条件时成立时执行语句体,

python  --- >> 格式:

  while( 判断条件 ):

     语句体;

 java:----  >> 格式:

  while( 判断条件 ){

     语句体;}

例:

  i =0

  while (i < 3):

    print "good"     // ---->>当i小于3时,输出good

java: ------ >>

  int i =0

  while (i < 3):

    System.out.println("good")     // ---->>当i小于3时,输出good

死循环:

while  True:

   print("我是死循环");

 java ------ >> 

while(true){

  System.out.println("我是死循环");

}

for 循环:

for  i in index:

  print i     // ------ >> 循环输出index的各字母:

 for i in range(0,10):

   print i   // ------- >>顺序输出0-9

'''

  range中可携带三个参数:例 range(0,10,2) 

  for i in range(0,10,2):

    print i     // ----- >>  隔行打数, 每两个数打印一次:

'''

二 循环案例:

  限定用户登录,每次登录失败提示剩余登录次数,登录成功调用猜数字游戏,  三次登录失败后提示账号锁定.三次猜数字机会,

  猜测成功和三次三次猜测错误都给与提示,是否想再玩一次?, 是则重新开始猜数字游戏, 否则退出游戏.程序结束:

python 版 

用户登录:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/14 13:06
# @Author : mixiu26
import guess as ges

# 定义已有用户名:
username = "admin"
password = "admin"
count = 0

while(count<3):# 用户输入账号密码:
name = input("name: ")
pwd = input("pwd: ")

# 对已有用户名进行校验:
if((username == name) and (password == pwd)):
count += 1
print("yes! you got it!")
Guess= ges.GuessAge()
Guess.guess()
break;
elif (2-count == 0):
# 输入失败的过程中需要再次进行判断, 是否已经到达第三次, 当到达第三次时要告知用户账户已锁定:
print("用户名或密码输入有误: ")
print("对不起, 您的账户已被锁定,请与管理员联系: ")
break;
else:
print("用户名或者密码输入有误: " + "您还有" +str((2 - count))+ "次机会")
count += 1

for 循环 guess age:
#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time : 2017/11/14 13:06
# @Author : mixiu26

age_of_girl = 52
count = 0
chance = 'y'
# for 循环格式: ---- >> for i in range(循环次数) --->> i就代表我们循环的变量次数
for num in range(0,99999999999999999999):
guess_age = int(input("guess age: "))
if count <= 2:
if guess_age == age_of_girl:
print("yes, you got it!")
count += 1
elif guess_age >= age_of_girl:
if count == 2:
print("sorry, your guess age were too large and sorry, you have no chance. ")
else:
print("sorry, your guess age were too large , sorry,please try once again:")
count += 1
elif guess_age <= age_of_girl:
if count == 2:
print("sorry, your guess age were too small and: sorry, you have no chance. ")
else:
print("sorry, your guess age were too small , sorry,please try once again:")
count += 1

if ((guess_age == age_of_girl) or (count == 3 )):
print("do you try once again? ")
choice = input("your Choice: ")
if(chance == choice):
count=0
else:
print("Thank you! ")
break;


while  --- >> 格式版

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/14 13:06
# @Author : mixiu26
age_of_girl= 52
count = 0
chance = 'y'
while count <= 3:
guess_age = int(input("guess age: "))
if count <= 2:
if guess_age == age_of_girl:
print("yes, you got it!")
count += 1
elif guess_age >= age_of_girl:
if count == 2:
print("sorry, your guess age were too large and sorry, you have no chance. ")
else:
print("sorry, your guess age were too large , sorry,please try once again:")
count += 1
elif guess_age <= age_of_girl:
if count == 2:
print("sorry, your guess age were too small and: sorry, you have no chance. ")
else:
print("sorry, your guess age were too small , sorry,please try once again:")
count += 1

if ((guess_age == age_of_girl) or (count == 3)):
print("do you try once again? ")
choice = input("your Choice: ")
if (chance == choice):
count = 0
else:
print("Thank you! ")
break;


1. java版代码实现:

import GuessGame.GuessNumber;

import java.util.Scanner;

/**
* Created by mixiu26 on 2017/11/14.
*
* 模拟登录, 限定三次机会,每次输入都提示下次剩余机会数:
* 登录成功后不立即退出,要求调用猜数字游戏:
*
*/
public class StringTest2 {
public static void main(String[] args) {
//定义已有用户名/ 密码:
String username = "admin";
String password = "admin";

//给定用户三次机会:
for (int x = 0; x < 3; x++) {
//创建键盘录入对象:
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名: ");
//键盘接收已输入用户名及密码:
String name = sc.nextLine();
System.out.println("请输入账户密码:");
String pwd = sc.nextLine();
//接收输入参数后将账户数据与原数据进行比对:
if (username.equals(name) && password.equals(pwd)) {
System.out.println("登录成功: ");
// break;
//调用猜数字游戏:
GuessNumber.star();
break;
} else {
//输入失败的过程中需要再次进行判断,是否已经到达第三次,当到达第三次时,要告知用户账户已锁定:
if (2 - x == 0) {
System.out.println("用户名或者密码输入有误: ");
System.out.println("对不起, 您的账户已被锁定,请与管理员联系: ");
} else {
//判断条件为用户名和密码都和原始数据相同时,才登录成功,所以else走的就是不相同的情况,不需要再加入条件判断:
System.out.println("用户名或者密码输入有误: " + "您还有" + (2 - x) + "次机会");
}
}
}
}
}

 guess 部分:

package GuessGame;

import java.util.Scanner;

/**
* Created by mixiu26 on 2017/11/14.
*/

public class GuessNumber {
private GuessNumber(){} // 私有限定,禁止构造调用

public static void star() {
System.out.println("进入猜数字小游戏");
int count = 0;
//产生一个随机数:
int number = (int)(Math.random() * 100) +1;
//键盘录入数据,与随机数进行比较: ---- >> 要求限定三次机会,三次机会后进行询问是否还要继续玩游戏:
Scanner sc = new Scanner(System.in);
String chance = "y";
for(; ;) {
System.out.println("请输入您猜测的数据: ");
int guessNumber = sc.nextInt();
//比较两个数据是否相等:
if(count<3) {
if (guessNumber == number) {
System.out.println("yes, you got it! ");
count += 1;
} else if (guessNumber > number) {
if (2 - count== 0) {
System.out.println("sorry, your guess age were too large and sorry, you have no chance.");
} else {
System.out.println("sorry, your guess age were too large , sorry,please try once again:");
}
count += 1;
} else {
if (2 - count == 0) {
System.out.println("sorry, your guess age were too small and sorry, you have no chance.");
} else {
System.out.println("sorry, your guess age were too small , sorry,please try once again:");
}
count += 1;
}
}
if ((guessNumber == number) || (count == 3)) {
System.out.println("do you try once again? ");
Scanner sc2 = new Scanner(System.in);
String choice = sc2.nextLine();
if(choice.equals(chance)){
count = 0;
}else {
System.out.println("Thank you!");
break;
}
}
}
}
}

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/mixiu26/p/7832189.html