Untitled
unknown
plain_text
3 years ago
4.4 kB
5
Indexable
1) private static int number_of_participants() { int tmp; int num; Console.WriteLine("enter no of students"); num = Convert.ToInt32(Console.ReadLine()); if (num < 1 || num > 9) { Console.WriteLine("this is not number. enter valid num"); num = Convert.ToInt32(Console.ReadLine()); } else if (int.TryParse(Console.ReadLine(), out tmp)) { Console.WriteLine("Enter no of students between 0 t0 9: "); tmp = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("Invalid: enter number of students"); num = Convert.ToInt32(Console.ReadLine()); return num; } Step 2/3 2) import random number = random.randrange(1, 10) guess = 0 count = 0 while guess != number and guess != "exit": guess = input("Please guess a number between 1 and 9. When you want to end the game print 'exit': ") if guess == "exit": print("Game Over") break guess = int(guess) count += 1 if guess not in range(1, 10): print("You have to guess a number between 1 and 9!") elif guess < number: print("You've guessed too low!") elif guess > number: print("You've guessed too high!") else: if count == 1: print("You rock! You've got it at the first try!") elif count <= 3: print("Well done! You've got it in just {} tries".format(count)) elif count > 3: print("You've got it! It took you {} tries!".format(count)) Step 3/3 //Declare and initialize LED pin variables int LED_1 = 2; int LED_2 = 3; int LED_3 = 4; //this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's print a start messgae to the serial monitor when a new sequence of random numbers starts Serial.println("Starting new Random Number Sequence"); //set the LED pins as outputs pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); //Let's make it more random randomSeed(analogRead(A0)); }//close setup //The loop() runs over and over again void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber); Final answer //Declare and initialize LED pin variables int LED_1 = 2; int LED_2 = 3; int LED_3 = 4; //this variable will hold a random number generated by the random() function long randomNumber; //Set up - this is where you get things "set-up". It will only run once void setup() { //setup serial communications through the USB Serial.begin(9600); //Let's print a start messgae to the serial monitor when a new sequence of random numbers starts Serial.println("Starting new Random Number Sequence"); //set the LED pins as outputs pinMode(LED_1, OUTPUT); pinMode(LED_2, OUTPUT); pinMode(LED_3, OUTPUT); //Let's make it more random randomSeed(analogRead(A0)); }//close setup //The loop() runs over and over again1 private static int number_of_participants() { int tmp; int num; Console.WriteLine("enter no of students"); num = Convert.ToInt32(Console.ReadLine()); if (num < 1 || num > 9) { Console.WriteLine("this is not number. enter valid num"); num = Convert.ToInt32(Console.ReadLine()); } else if (int.TryParse(Console.ReadLine(), out tmp)) { Console.WriteLine("Enter no of students between 0 t0 9: "); tmp = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("Invalid: enter number of students"); num = Convert.ToInt32(Console.ReadLine()); return num; } void loop() { //generate a random number randomNumber = random(2,5); //display the random number on the serial monitor Serial.print("The Random Number is = "); Serial.println(randomNumber); Hope the code is helpful... Thank you Was this answer helpful? Post a question
Editor is loading...