I am writing a simple program to check the grade of a person and output what Letter it is. For example I want to check if the user entered a number between 60-69 and if so out put a 'D'.
Any answers would be helpful, Thank you
If you want to ask user to enter something into the consol you need, first, say him what to do by using std::cout
After that you should get what he inserted into the consol by using std::cin.
After that you can compare the value with different condition to get the letter you want(the grade). A simple way can be like user "Beta" put on comments.
if(n < 60)
grade = "E";
else if(n >=60 && n <= 69)
grade = "D";
else if(....
...
Related
I have a PDF form that adds up several different answers and displays the sum of these answer at the bottom of the page. I want to take that number, and have a sentence underneath it display three different options depending on the sum.
Greater than 60: Proceed
between 45 & 60: Consult Sales Lead
Less than 45: Decline
I am attempting to run a custom calculation script that takes the sum (which is named "total") and writes the above options, but I'm running into a myriad of errors.
My code I've written is below
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
} else {
if (A <= 45){
event.value = "Decline";}
} else {
if (A < 60 && A > 45){
event.value = "Proceed, decision made with sales leader";}
}
If I were to only write the below block, I do not get any errors.
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
}
I'm a newbie when it comes to most JavaScript, so any help is greatly appreciated! Thanks in advance!
I have based most of my code off of different search results from google. My main source
example 1
Below are a few other links I've referenced
example 2
example 3
You can leave away the
if (A < 60 && A > 45) {
line, because that condition is always true after the two previous conditions.
And then, it should work properly.
Just if you want to stay with that last condition, you would have to close it with a curly brace.
I think I managed to figure it out! For some reason, the addition of the "else" factor was causing syntax errors, so I tried it without and it seems to work as intended!
My code, for anyone that happens to find this, is the following:
var A = this.getField("total").value;
if (A >= 60) {event.value = "Proceed";}
if (A <= 59 && A >= 46) {event.value = "Proceed, decision made with sales leader";}
if (A <= 45) {event.value = "Decline";}
if (A == 0) {event.value = " ";}
Thanks to everyone that took a look at this, even if you didn't get to comment before I figured it out!
as you can see down it's not giving the value of input to the if! why? and how i can make the value of input be stored to Variable (number)
number = input(f"Enter a Number? ")
if number == (1):
print("one")
if number == (2):
print("two or greater")
else:
print("negative number")
Enter a Number? 1
negative number
There are some issues, usually when you use input and expect a number to be submitted, you want to use int() before it to make sure the input is understood as a number (type int). Furthemore, when you want to state an "else-if" statement you should use elif. Finally, the parenthesis are not necessary in your if statement. The following should work:
number = int(input("Enter a Number? "))
if number == 1:
print("one")
elif number >= 2: #I've changed this syntax, because two or greater is written like this
print("two or greater")
else:
print("negative number")
Here is the output I'm trying:
Enter a Number? 3
two or greater
I am a beginner in Python and I was trying to write a small program that asks the user to enter an integer that is greater than 0. The function should keep on asking the user for the number until it is valid.
I tried something like below, however I am getting the wrong results. Can you please help me understand my error?
num = input('please enter a number: ' )
n = int(num)
while n < 0:
num = input('please enter a number: ' )
if n >= 0:
print('Valid number')
else:
print('Invalid number')
Is it possible to start the code without an input function? (like to start with num = int())
Thank You for Your Time
There's an error with the logic behind your code.
You firstly ask the user for a number and if he inputs a number which is greater than or equal to 0, the while-loop will never start (in your script: while n < 0:), which, I assume is fine, because the goal of your program is, as you said, to make "the user to enter an integer that is greater than 0".
If the user inputs a number that is smaller than or equal to 0, the while-loop will start, but will never break because inside of it, the value of variable n never changes, only does the value of num.
This is an appropriate script considering that you want to make the user input a number greater than 0, and that you want to give feedback regarding their input.
n = None
while n <= 0:
n = int(input('please enter a number: '))
if n <= 0:
print('Invalid number')
else:
pass # the loop will break at this point
# because n <= 0 is False
print('Valid number')
The code has the user stuck in a loop until they write a number that's greater than 0.
Another solution would be to, inside the loop, check whether int(num) is greater than 0 and if it is, print 'Valid number' and do break to stop the loop; if it's not, print 'Invalid number' (though then the loop doesn't need to be defined by while n < 0:; rather by while True:.
Also, what do you mean by this:
Is it possible to start the code without an input function? (like to start with num = int())
Please clarify this part.
If your problem is the code not terminating, writing Invalid number all the time it's because you are not updating the value of n. You assigned it just once. The solution to your problem is as follows:
n = -1
while n < 0:
n = int(input('please enter a number: '))
if n >= 0:
print('Valid number')
else:
print('Invalid number')
By the way you get rid of starting the code without an input function.
Edit:
As you just said - you want to keep the input reading despite passing an negative integer into the command line. This should help you accomplish this:
while True:
n = int(input('please enter a number: '))
if n >= 0:
print('Valid number')
else:
print('Invalid number')
This loop will go forever until you exit the program lets say with ctrl + C. while True: is as you see an forever ongoing loop, because the True argument will never be false.
So I've got this code, it guesses a number between 0 and 100.
It prompts a number and the user answers with 'h' 'l' or 'c'
now everything works fine except that I want this line to only show when the user input is not equal to 'h', 'l' or 'c' instead now it shows up even if I enter 'c' which would break the loop, right?
print "Sorry, I did not understand your input."
full code
start = 0
end = 100
guess = int((start+end)/2.0)
print "Please think of a number between 0 and 100!"
print
x= 'n'
while x != 'c':
print 'Is your secret number ' + str(guess) + '?'
x = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.").lower()
if x == 'h':
end = guess
elif x == 'l':
start = guess
else:
print "Sorry, I did not understand your input."
guess = int((start + end)/2)
print 'your answer is: ' + str(guess)
So to clarify this is the output even when I enter 'c'
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.c
Sorry, I did not understand your input.
your answer is: 50
You do not have an elif for the 'c' case, so it falls to the final else.
Add, the following to handle that case and break the while loop:
elif x == 'c':
break
In addition, you can just do while True: and loop until c breaks.
Your higher/lower logic is backward as well. for h, start = guess, for example.
The update to guess should be indented inside the while loop.
Hello I am fairly new to python and would like to know where my program is failing and why.. thanks, the basic coding is as follows,
grade = 0
total = 0
scorecount = 0
while grade >=0:
grade = raw_input("enter grade ->")
grade = int(grade)
total = total + grade
total = int(total)
scorecount = scorecount + 1
scorecount= int(scorecount)
average = total/scorecount
print average
You accept the grade, then add it to the average even if it is -1, since you don't check for -1 until the loop starts again.
To quit a loop halfway through, use break. Then, you can write
while True: # loop 'forever' until break
grade = raw_input("enter grade ->")
grade = int(grade)
if grade == -1:
break # we're done
# rest of processing...
Your checking for -1 AFTER you change it, so you should check that the raw grade is that and break from the loop before processing.
It'd be smarter to use a list to manage something like this.
grades = []
while True:
grade = int(raw_input('Enter a grade: '))
if grade < 0:
break
grades.append(grade)
print '\nAverage:', float(sum(grades)) / len(grades)
There are better ways to have the user break the loop than entering a negative grade, but there you go.