ParseError: bad input on line 5 [duplicate] - python-3.6

This question already has an answer here:
Python 3.6 quit() not working after porting to exe
(1 answer)
Closed 2 years ago.
score = raw_input("Enter Score: ")
try:
s = float(score)
except:
print "Error"
quit()
if s >= 0.9:
print "A"
elif s >= 0.8:
print "B"
elif s >= 0.7:
print "C"
elif s >= 0.6:
print "D"
else:
print "F"

In place of quit() i recommend you to use sys.exit(0)
import sys
score = raw_input("Enter Score: ")
try:
s = float(score)
except:
print "Error"
sys.exit(0)
if s >= 0.9:
print "A"
elif s >= 0.8:
print "B"
elif s >= 0.7:
print "C"
elif s >= 0.6:
print "D"
else:
print "F"
if you are using python 3 then use parenthesis with print function
** Best Way **
score=input()
try:
s=float(score)
if(s>=0.9):
print("A")
elif(s>=0.8):
print("B")
elif(s>=0.7)
print("C)
except:
print("error")

Related

Why does Base.rsplit not invert the order (compared to Base.split) of the data in Julia?

I am trying out Base.rsplit() for the first time and I was surprised to see that the order of the data does not change when I use split vs rsplit. See this example:
julia> my_string = "Hello.World.This.Is.A.Test"
"Hello.World.This.Is.A.Test"
julia> a = split(my_string, ".")
6-element Vector{SubString{String}}:
"Hello"
"World"
"This"
"Is"
"A"
"Test"
julia> b = rsplit(my_string, ".")
6-element Vector{SubString{String}}:
"Hello"
"World"
"This"
"Is"
"A"
"Test"
julia> a == b
true
This is a bit counterintuitive given that rsplit says:
Similar to split, but starting from the end of the string.
rsplit just goes from the right side and the only practical difference is the limit parameter.
Try typing in Julia:
#less rsplit("txt",".")
You will find the following function:
function _rsplit(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
n = lastindex(str)::Int
r = something(findlast(splitter, str)::Union{Nothing,Int,UnitRange{Int}}, 0)
j, k = first(r), last(r)
while j > 0 && k > 0 && length(strs) != limit-1
(keepempty || k < n) && pushfirst!(strs, #inbounds SubString(str,nextind(str,k)::Int,n))
n = prevind(str, j)::Int
r = something(findprev(splitter,str,n)::Union{Nothing,Int,UnitRange{Int}}, 0)
j, k = first(r), last(r)
end
(keepempty || n > 0) && pushfirst!(strs, SubString(str,1,n))
return strs
end

Why does Julia not print `false` inside a conditional?

Julia 1.4.2
The behaviour that stumps me occurs in the first and fourth cases:
julia> if (val = (1 == 2))
println(val)
end
julia> if (val = (1 == 1))
println(val)
end
true
julia> if (val = (1 == 1))
println(val + 5)
end
6
julia> if (val = (1 == 2))
println(val + 5)
end
julia> 1 == 2
false
julia> 1 == 1
true
julia> println(1==2)
false
julia> println(1==1)
true
I observe this in the REPL and in Jupyter.
My questions are:
Why does this happen?
How can I retrieve the value of val, preferably the "truthy" value, in both cases?
Let's look at the first example in more details
# initially, `val` does not exist
julia> val
ERROR: UndefVarError: val not defined
# 1==2 is false
# the assignment returns the value of the right-hand side -> false
julia> if (val = (1 == 2))
# therefore, this branch is not taken
println(val)
else
# but this one is
println("in the else clause")
end
in the else clause
# `if` returns whatever the branch taken evaluates to.
# In this case, `println` returns nothing, so the `if` block does not
# return anything either
julia> #show ans
ans = nothing
# `if` doesn't introduce a new scope, so the newly created `val` variable
# can still be inspected
julia> val
false

Diagram evaluation with 3 possible results

Is there a way to check the color of this diagram without using too much if?
Not like this:
if(x == "b" && y == "b"){ return "red";}
if(x == "b" && y == "e1" || x == "b" && y == "e2" .....){ return "green";}
........
I think there must be a way to simple calculate the result with given values for the characters, but I can't find it.
Something like checking x+y=z or x*y=z. Where z can be one of three numbers.
The language doesn't matter.
Thanks
I'd recommend targeting the red diagonal and green column, picking off individual cases and then returning yellow:
if x == "i"{ return "yellow"}
if y == "i"{ return "yellow"}
if x[0] == y[0]{ return "green"}
/*
target remaining green cases
*/
return "yellow"

Check if number is in a cut type interval or range [duplicate]

This question already has answers here:
Why are these numbers not equal?
(6 answers)
Closed 3 years ago.
My goal is to check if a given number is in a cut type interval or range like this: "[1.86e+03,2.43e+03]". I get the interval as a string.
I'm having trouble with the higher end of the interval for some reason and can't see what I'm doing wrong, the lower seems to work fine and I haven't included it in the code below.
Here's a part of the code that doesn't seem to work:
library(readr)
IsNumLess <- function(num, interval) {
intervalL <- unlist(strsplit(interval, ",")) #split on comma
lastSt <- intervalL[2] #get the whole second part
lastNum <- parse_number(lastSt) #get just the number, without ) or ]
if (endsWith(lastSt, ']')) #up to and including
{
if (!(num <= lastNum))
{
print(num)
print(lastNum)
print(num <= lastNum) #this and line below should return the same value
print(2430 <= 2430)
print("f3")
return(FALSE)
}
}
else # ) - up to but not including
{
if (!(num < lastNum))
{
print("f4")
return(FALSE)
}
}
return(TRUE)
}
If I run this IsNumLess(2430, "[1.86e+03,2.43e+03]") it comes back as FALSE, and it should be TRUE as 2430 <= 2.43e+03...
> IsNumLessMin(2430, "[1.86e+03,2.43e+03]")
[1] 2430
[1] 2430
[1] FALSE
[1] TRUE
[1] "f3"
[1] FALSE
Edit:
Thank you G5W, the duplicate question link got me to where I needed.... This line worked in the second 'if':
if (!(num < lastNum | isTRUE(all.equal(num, lastNum))))
This is a problem with the accuracy of representing decimals in a computer. See here for an in depth discussion.
If you run:
sprintf("%.54f",num)
sprintf("%.54f",lastNum)
You will see that the num != lastNum .
An easy fix in your code is to replace print(num <= lastNum) with print(num < lastNum | all.equal(num, lastNum)).

Return result from multiple if statements

I'm trying to use multiple actions in if statement. For example:
x <- 1
if (x == 1) {
paste("First")
1*1 #multiple actions
} else if (x == 2) {
paste("Second")
2*2 } else {("Nothing")
}
[1] 1 #what I'm getting
[2] "First"
1 #what I want to get
In this case only the second part of the expressions was printed to the console.
Any ideas how can I run all actions between if and else if ?
All statements are running as intended. The value of a statement is only printed to the console if all these conditions are true:
The value isn't saved to a variable
The value isn't invisible
It's the result of the last statement in an expression
R is running in interactive mode
The reason things sometimes print is to help people interactively exploring data in the command line. Instead of type print(x), they can save a few keystrokes by just typing x. In summary, use print if you want to be sure it's printed:
x <- 1
if (x == 1) {
print("First")
print(1*1)
} else if (x == 2) {
print("Second")
print(2*2)
} else {
invisible("Nothing")
}
# [1] "First"
# [1] 1
You can use print or cat:
getResult <- function(x = 1) {
if (x == 1) {
cat("First", 1 * 1, "\n")
} else if (x == 2) {
print("Second")
print(2 * 2)
} else {
cat("Nothing\n")
}
}
getResult()
# First 1
getResult(2)
# [1] "Second"
# [1] 4

Resources