Question

In: Computer Science

Code in python Write a while loop code where it always starts form 2. Then it...

Code in python

Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back as well, so the program can't choose 2 and then 2 again.

The output should look like this:

Code Started, at number 2

Chosen number is 3

Chosen Number is 2

Chosen Number is 5

TP

Rerunning program

Code Started, at number 2

Chosen Number 1

Sl

Rerunning program

Solutions

Expert Solution

def run():
  print("code started at number 2")
  num = int(input("Choosen number is"))
  first_num = 0
  while(num <=5 and num != first_num):
    if num == 4 :
      print("TP")
      first_num = num
      num = int(input("Choosen number is"))
    elif num == 1:
      print("SL")
      first_num = num
      print("Rerunning program")
      run()
    elif num == 5:
      first_num = num
      print("Rerunning program")
      run()
    elif (num == 2 or num == 3):
      first_num = num
      num = int(input("Choosen number is"))
    else:
      break
      
    
      
run()

Output -

Given Test cases:

this is the given case tested.

In the given output when 5 is hit , it prints TP and in question you gave that TP will print when hit 4. That's why I am not printing TP when hit is 5.

If you want the case, you can add print("TP") under the condition if num == 5:

Next test cases:

if same two numbers are given and given a number other than 1 to 5:

I coded the program if these two conditions happen, program just end. but if we want to rerun the program again, simply it can run as:

def run():
  print("code started at number 2")
  num = int(input("Choosen number is"))
  while(num <=5 and ):
    if num == 4 :
      print("TP")
      num = int(input("Choosen number is"))
    elif num == 1:
      print("SL")
      print("Rerunning program")
      run()
    elif num == 5:
      print("Rerunning program")
      run()
    elif (num == 2 or num == 3):
      num = int(input("Choosen number is"))
    else:
      break
      
    
      

I think you get my points, if you need more clarification or want to satisfy more conditons please COMMENT, I will update it too.


Related Solutions

PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every...
PUT IN PYTHON LANGUAGE CODE # Write one while-loop that starts at 500 and prints every 6th number down to 300 # (i.e., prints 500, 494, 488, . . . etc., but does not print any number lower than 300). # Write one while-loop that starts at 80 and prints every 12h number thereafter, # but does not print any number greater than 210 # Write one while-loop that prints all the numbers from 30 through 70, # except for...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Write an example of a Python while loop that is infinite, calls the functions first(), second()...
Write an example of a Python while loop that is infinite, calls the functions first(), second() and third() in that order in the body of the loop, but skips calling second() and third() if first() returns a value of False, and exits if second() returns a value of False. Declare a Python function named sumEm() that accepts three numeric parameters and returns the sum of the three numbers. Assume sumEm() is declared in a module named myMod.py, which your program...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000...
Where is the infinite loop in the code? Input for the code 1 2.5 2 40200000 1 0 2 80400000 2 ffffffff 3 #include <stdio.h> #include <math.h> #include <stdlib.h> int count = 31; void toBinary(int num, int n){ for(int i = 1; i < n; i++){ if((int)(num/pow(2,(n-i))) > 0){ num = num - pow(2,(n-i)); printf("1"); count--; }else{ printf("0"); count--; } } } char checkSign (int sign) { char new_sign; if (sign == 0) { new_sign = '+'; return new_sign; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT