How can I use the while loop to create an endless loop of user input-->tells me if int is odd or even? - while-loop

number = ''
number = int(input("Please input a number and I will tell you if it is odd or even.\n"))
while True:
if (number % 2) != 0:
print("This is an odd number.")
else:
print("This is an even number.")
#I tried this loop below
user_input = ""
while user_input != "exit":
user_input = int(input("Please input another number and I will tell you if it is odd or even.\n"))
number = user_input
This is only telling me if the first number is odd or even but not the following numbers

Related

Looking for a fast way, to represent the value of an integer, by a number in the range 0 to 3 (without branching?)

Given that val is some random integer,
and number the possible outcome:
if the value is less then &H100 ; the number is 0
if the value is less then &H10000 ; the number is 1
if the value is less then &H1000000 ; the number is 2
else ; the number is 3
I've got this:
If (val And &HFFFF0000) = 0 Then
If (val And &HFF00) = 0 Then
num = 0
Else
numb = 1
End If
ElseIf (**val** And &HFF000000) = 0 Then
numb = 2
Else
numb = 3
End If
I believe to remember that I could achieve this with a simple calculation, but I can not
wrap my head around it...
cheers..
Jhonny
edit:--- after reaction of video.baba ---
Here is half a solution:
The result is a number from 0 to 7, of witch the bits represent a non-zero byte.
A lookuptable could be used to translate it to a 2-bit value.
val >>= 8 ' move to the right, so the first byte can hold identification-bits
val += &H3FF0000 'set a bit in the first byte, if the second one is not zero
val = val And &H400FFFF
val += &H1FFFF00 'set a bit in the first byte, if the third one is not zero
val = val And &H60000FF
val += &HFFFFFF'set a bit in the first byte, if the fourth one is not zero
val >>= 24 'put the result in the last byte
number=lookuptable(val)
have not tested it for speed yet, but it feels over-complicated?
Do you mean something like:
Select Case Value
Case < &H100
Number = 0
Case < &H10000
Number = 1
Case < &H1000000
Number = 2
Case Else
Number = 3
End Select

Simple Beginner Loop Wont Close

Practicing some stuff and I've been way beyond this simple of a concept but this loop won't close and I can't understand why.
ans = "Y"
while ans == "Y" or "y":
num = int(input("What's your number? "))
if num % 2 == 0:
print("That number is even!\n")
else:
print("That number is odd!\n")
ans = str(input("Do you have another number, Y/y or N/n? "))
So I declare the variable first so I can enter the loop then ask for it again on the tail side to close it out...but no matter what I enter it continues.
I'm sure it's simple like I said i'm way past this kind of thing but it won't close and I'm not sure what the issue is?
The problem lies in the 'or "y"':
You should check if ans is Y or y which is correctly expressed as 'ans == "Y" or ans == "y"'
You as well needs to specify a starting condition for ans.
ans = "y"
while ans == "Y" or ans == "y":
num = int(input("What's your number? "))
if num % 2 == 0:
print("That number is even!\n")
else:
print("That number is odd!\n")
ans = str(input("Do you have another number, Y/y or N/n? "))

while True: try: except ValueError:

I want to make a function like this:
while True:
try:
x = int(input("Please enter a number 1 - 5: "))
print (x)
except ValueError:
print "Oops! That was not a valid number. Try again..."
but how can I make sure that ‘x’ only will be printed if x is between 0 – 6.
If you want to keep the exception flow, you can raise an exception when the user inputs an invalid value.
while True:
try:
x = int(input("Please enter a number 1 - 5: "))
if x < 1 or x > 5:
raise ValueError('Input not valid') # go to except block
print (x)
except ValueError:
print ("Oops! That was not a valid number. Try again...")
You can also write a loop without raising an exception:
while True:
x = int(input("Please enter a number 1 - 5: "))
if x < 1 or x > 5:
print ("Oops! That was not a valid number. Try again...")
continue # skip print
print (x)

Understanding the steps in making a counter for each letter when a sentence is inputed

I have an example of a program that shows how to set up a counter for how many times each letter of the alphabet was used. I don't understand the syntax of the middle portion of the program.
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
I have tried searching on youtube and tutorials online
CLS
REM Make Counters for each Letter!!!
DIM Count(ASC("A") TO ASC("Z"))
REM Get the Sentence
INPUT "Enter Sentence:", sentence$
LET sentence$ = UCASE$(sentence$)
FOR I = ASC("A") TO ASC("Z")
LET Count(I) = 0
NEXT I
FOR LETTERNUMBER = 1 TO LEN(sentence$)
LET letter$ = MID$(sentence$, LETTERNUMBER, 1)
IF (letter$ >= "A") AND (letter$ <= "Z") THEN
LET k = ASC(letter$)
LET Count(k) = Count(k) + 1
END IF
NEXT LETTERNUMBER
PRINT
REM Display These Counts Now
LET letterShown = 0
FOR letternum = ASC("A") TO ASC("Z")
LET letter$ = CHR$(letternum)
IF Count(letternum) > 0 THEN
PRINT USING "\\## "; letter$; Count(letternum);
END IF
LET letterShown = letterShown + 1
IF letterShown = 7 THEN
PRINT
LET letterShown = 0
END IF
NEXT letternum
END
A through Z appears with the count of how many times they appeared.
The MID$ function returns a portion of a STRING's value from any position inside a string.
Syntax:
MID$(stringvalue$, startposition%[, bytes%])
Parameters:
stringvalue$
can be any literal or variable STRING value having a length. See LEN.
startposition%
designates the non-zero position of the first character to be returned by the function.
bytes%
(optional) tells the function how many characters to return including the first character when it is used.
Another method to calculate characters in a string:
REM counts and displays characters in a string
DIM count(255) AS INTEGER
PRINT "Enter string";: INPUT s$
' parse string
FOR s = 1 TO LEN(s$)
x = ASC(MID$(s$, s, 1))
count(x) = count(x) + 1
NEXT
' display string values
FOR s = 1 TO 255
PRINT s; "="; count(s); " ";
IF (s MOD 8) = 0 THEN
PRINT
IF (s MOD 20) = 0 THEN
PRINT "Press key:";
WHILE INKEY$ = "": WEND: PRINT
END IF
END IF
NEXT
END

Quickest way to determine if a number is a prime number or not VB

I have been working on this for quite a bit now, I have a task of creating a program which calculates if a number entered by a user is prime number or not, the program calculates the time taken and displays this to the user, however I have found two method, one takes more time than the other but it produce accurate numbers, the other one calculates very quickly however it is wrong, I am hoping if someone can help me and tell me the quickest way of calculating this, here are my two codes
Code1:
Dim ch As String
ch = "y"
While ch = "y"
If (num Mod 2 = 0) Then
Console.WriteLine("Is not a prime number!")
Else
Console.WriteLine("Is a prime number!")
End If
Code2:
check = 1 'initilizing a check point to use it in the program to determine prime number
Dim Value As Long
Console.Write(vbLf & "Enter a number To check Whater it is Prime or Not :")
Value = Long.Parse(Console.ReadLine())
start_time = Now
Dim ch As ULong
ch = 0
Dim i As ULong
i = 2
While (i <= Value / 2)
If (Value Mod i = 0) Then
ch = 1
Exit While
End If
i = i + 1
End While
If (ch = 0) Then
Console.WriteLine("Prime Number")
Else
Console.WriteLine("Not Prime Number")
End If
There are a great many prime testers out there, many of them on this site. For checking a single number I use a faster variant of your Code2 with a little extra checking. Here is the pseudocode:
boolean function isPrime(num)
//1, 0 and negatives cannot be prime.
if (num < 2) then
return false
endif
// 2 is the only even prime.
if (num MOD 2 = 0) then
return (num = 2)
endif
// Check for odd factors.
limit <- sqrt(num)
for (factor <- 3; factor <= limit; factor <- factor + 2) do
if (num MOD factor = 0) then
return false
endif
endfor
// If we reach this point then the number is prime.
return true
endfunction
As #user448810 said, you should use the square root of your target number as the limit of your testing loop. You can basically halve the number of tests you do by treating even numbers separately. Once you have taken out the even numbers, then you only have to test odd factors: 3, 5, 7, ...